jm2
jm2

Reputation: 763

ExtJS4 Controller not getting store (null)

I have a ProjectsController like:

Ext.define('SimpleTodo.controller.ProjectsController', {
    extend: 'Ext.app.Controller',
    refs: [
        { ref: 'addProjectPanel', selector: '#addProjectPanel' }
    ],
    init: function() {
        this.control({
            'button[action=addProject]': {
                click: this.addProject
            }
        });
    },
    addProject: function() {
        var form = this.getAddProjectPanel().getForm();
        if (form.isValid()) {
            var projectsStore = Ext.data.StoreManager.lookup('projects'); // Am getting null here
            projectsStore.add(form.getFieldValues());
            projectsStore.sync();
        } else {
            Ext.Msg.alert('INVALID!');
        }
    }
});

I believe the problem is because the store has not been initialized yet? How then should I modify my code? My store is defined like:

Ext.define('SimpleTodo.store.Projects', {
    extend: 'Ext.data.Store',
    requires: [
        'SimpleTodo.model.Project'
    ],
    model: 'SimpleTodo.model.Project',
    storeId: 'projects'    
});

I encounted the same problem when trying to load my store, thus populating my Gridview/Table. I fixed it by moving the function to launch, wonder if thats the right way? In the docs, init is used

Ext.define('SimpleTodo.controller.SidebarController', {
    extend: 'Ext.app.Controller',
    ...
    launch: function() {
        var projectsStore = Ext.data.StoreManager.lookup('projects');
        projectsStore.load();
    },

Upvotes: 2

Views: 1637

Answers (2)

Michael Steinhart
Michael Steinhart

Reputation: 26

Add

stores:
[
  'Projects'
],

to controller code and then you can use

getProjectsStore();

to refer to. Have a look at http://docs.sencha.com/ext-js/4-0/#!/guide/mvc_pt3, 1. Getting References

Upvotes: 1

Hataru
Hataru

Reputation: 94

Have u config a proxy to load the data?

Anyway place models: [name of the model], stores:[ name of the store],

into controller

Set into Store autoLoad:true;

Set a Proxy into Model to tell how load data into store.

Ref: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.proxy.Proxy

Upvotes: 0

Related Questions