ember.js - Differences Regarding created vs extended ember controllers; their naming conventions and scope -
right i'm hitting wall in understanding of ember controllers.
i have model "batch" never main model of route or controller. want able access objects of class via arraycontroller route in app.
therefore created empty batches controller nil more than
// controllers/batches.js app.batchescontroller = ember.arraycontroller.create();
then created batches initializer contains
// initializers/batches.js ember.application.initializer({ name: 'batch', after: 'preload', initialize: function (container, application) { var store; store = container.lookup('store:main'); store.find('batch', { state: "uploaded" }).then(function (batches) { app.batchescontroller.set('content', batches.content); }); } });
note after lot of heartache, figured out setup worked capital b in batches controller although coworker had read should infact lower case , have no thought why either of 2 things important.
this setup works until need reference content of controller in controller. here sec controller:
// controllers/inbox.js app.inboxcontroller = app.librarycontroller.extend({ needs: ['application', 'batches'], hasactivebatches: function () { // here this.get('controllers.batches') ==> typeerror: undefined not function }.property('controllers.batches') });
instead can access content of batches controller via "app.batchescontroller" of no utilize creating computed property.
i realize fundamentally different controller because manually created instead of extended , instantiated ember don't understand difference or how affects options accessing it's content.
any clarification going on behind scenes here , perchance improve pattern utilize here much appreciated.
controller classes should capitalized. shouldn't created, extended. when using needs should specify in camelcase. when ember creates controller keeps track of , makes available other controllers via needs, if create it, doesn't know it. using initializer weird here, it'd create more sense in application controller batches , set controller.
i'd this, create array, , attach of controllers. access property right on controller without having utilize needs or (you inject on of routes if wanted).
ember.application.initializer({ name: 'batch', after: 'preload', initialize: function (container, application) { var store = container.lookup('store:main'), batchesarr = []; application.register("my:batches", batchesarr, {instantiate: false}); application.inject("controller", "batches", "my:batches"); store.find('batch', { state: "uploaded" }).then(function (batches) { batchesarr.pushobjects(batches.toarray()); }); } });
example: http://emberjs.jsbin.com/nobima/8/edit
example of everyone has same collection, 1 updates, update: http://emberjs.jsbin.com/nobima/9/edit?html,js,output
example using controllerfor: http://emberjs.jsbin.com/nobima/11/edit
ember.js
No comments:
Post a Comment