最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Exj JS 5, Access StoreModel from ViewController - Stack Overflow

programmeradmin1浏览0评论

I want to load item/items from ViewController. Here is an example:

Ext.define('MyApp.model.User', {
   extend: 'Ext.data.Model',
   fields: ['name', 'email'],
   proxy: {
       type: 'rest',
       ...
   }
});

Ext.define('MyApp.store.User', {
   extend: 'Ext.data.Store',
   model: 'MyApp.model.User',
   data : [
    {firstName: 'Seth',    age: '34'},
    {firstName: 'Scott', age: '72'},
    {firstName: 'Gary', age: '19'},
    {firstName: 'Capybara', age: '208'}
   ]
});

Ext.define('MyApp.view.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

    init: function() {
        //how to access store here and load items, using its load() method?
        //how to access model here and load an item, using its load() method?
    }
});
  1. How to access store from here and load items, using its load() method?
  2. How to access model and load an item using its load method?

I want to load item/items from ViewController. Here is an example:

Ext.define('MyApp.model.User', {
   extend: 'Ext.data.Model',
   fields: ['name', 'email'],
   proxy: {
       type: 'rest',
       ...
   }
});

Ext.define('MyApp.store.User', {
   extend: 'Ext.data.Store',
   model: 'MyApp.model.User',
   data : [
    {firstName: 'Seth',    age: '34'},
    {firstName: 'Scott', age: '72'},
    {firstName: 'Gary', age: '19'},
    {firstName: 'Capybara', age: '208'}
   ]
});

Ext.define('MyApp.view.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

    init: function() {
        //how to access store here and load items, using its load() method?
        //how to access model here and load an item, using its load() method?
    }
});
  1. How to access store from here and load items, using its load() method?
  2. How to access model and load an item using its load method?
Share Improve this question asked May 8, 2015 at 5:24 AlexandrAlexandr 9,54512 gold badges68 silver badges110 bronze badges 4
  • Note that all you've done there is define a store type. To use it, you would need to create an instance, which you could do inside the init method. – Evan Trimboli Commented May 8, 2015 at 6:01
  • @Evan Trimboli, Ext JS creates store itself when it is used via application controller. You just add a stores array and you can use this.getSomeStoreNameStore() from controller. You don't create it manually. Why should I create a store manually when it is used inside view controller? I think there is a way, which I don't know. – Alexandr Commented May 8, 2015 at 6:10
  • Because it's a view controller, which means it's on a per view basis (you could have many instances). An application controller is a one-off that creates a single store instance of a particular type shared throughout the app. You might want to look at the view model stores config. If you're not using a view model, then you need to create an instance. – Evan Trimboli Commented May 8, 2015 at 8:08
  • @Evan Trimboli: what's responsibility of view controller in this case? Should it delege CRUD operations to application controller? Could you give some elementary example with a html form, include load, update operations, put event handlers in view controller, without view model elements, please? By the way, I use view, inside it there is a 'store: 'User'' property, but when I try in view controller get it via: this.getView().getStore(), result is undefined. – Alexandr Commented May 8, 2015 at 8:55
Add a ment  | 

1 Answer 1

Reset to default 6
this.getView().getStore() // get the store from the controller's view
this.getStore('name') // get store by name
this.getViewModel.getStore('store key') // get store from the View model using the key of the store

You can even do,

// You can use these from anywhere in the application 
Ext.getStore('Store name');
Ext.data.StoreManager.lookup('Store name');
AppName.app.getModel('Model Name').create();
Ext.ClassManager.get('MyModel');
Ext.data.schema.Schema.instances.default.getEntity('MyModel');

"A view controller is a controller that can be attached to a specific view instance so it can manage the view and it's child ponents. Each instance of the view will have a new view controller, so the instances are isolated."

A ViewController has to be associated to a View. Without a view, it serves no significant purpose. A ViewController is used to handle all controls related to its view: routing, event handlers... And if you are using the MVVM design, you should also use a ViewModel for your view ( not really mandatory though). If you've setup your MVVM properly, then you can get the instance of the associated stores and models easily with the default methods already provided by your ViewController and ViewModel.

getStore( name ) : Ext.data.Store Get a Ext.data.Store attached to the ViewModel attached to this controller. See Ext.app.ViewModel.getStore.

Please check the MVVM architecture. I also find Saki's examples very useful: http://extjs.eu/ext-examples/#plex-data-binding-5

发布评论

评论列表(0)

  1. 暂无评论