简体   繁体   中英

Ember.js v1.0.0-rc.1 — How do I get/supply a controller for a programmatically created view?

I'm migrating my application from Ember v1.0.0 pre3 to rc1, and it seems I can no longer grab a global routers instance, nor can I get a controller within a view I create manually.

I have a number of modal screens that don't use routes at the moment (TODO), but for my deadline this week, I need to just get these modals displaying and populating.

I'm creating my modals like:

App.helpModalView = App.HelpModalView.create();
App.helpModalView.append(); 

and handle the modal display in the view's didInsertElement method:

didInsertElement: function() {
   // miscellaneous setup operations
   // ...
   this.$().modal({backdrop:"static"});
},

but inside the actual view class

this.get('controller') == null

Inspecting my various objects, it looks like 'controllerFor' is only available on routers, and I used to grab this info from App.router.get('controllerName')

How can I get a controller from a programmatically created view?

On a similar note, what is the best way to supply a controller for a view I haven't accessed yet? This project has an embarrassing amount of logic in the view layer, that I would like to push into the associated controller.

I had a similar scenario and solved it like this:

            IB.TreeNavController = Em.Controller.extend({
                container: IB.__container__,
                init: function() {
                  this._super();
                }              
            });

            IB.TreeNavView = Em.View.extend({
                templateName: 'treeNav',

                init: function() {
                    this._super();
                    var ctrl = IB.TreeNavController.create();
                    this.set('controller', ctrl);
                },
            });

So I've defined a Controller which is instantiated in the init() of the view. The view can be instantiated programmatically. IB is the name of the app in this example.

When you render something like this:

 this.render('treeNav', { controller: this.controllerFor('treeNav')};

this.controllerFor could be anything what you want - even one that is not assigned to any view yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM