Ember - how to create and bind a Checkbox controller?

Deadly 提交于 2019-12-24 23:26:33

问题


This question is linked to the answer given here.

Having a checkbox in a view

App.RoleCheckbox = Em.Checkbox.extend({
    userRolesBinding: 'parentView.user.roles', // Points to the roles of the user

    checked: function () {
        var userRoles = this.get('userRoles');
        return userRoles.contains(this.get('content'));
    }.property('content', 'userRoles.@each'),

    click: function (evt) {
        //do something
        var controller = this.get("controller");
        controller.clicked(evt);
     }
});

I would like that the click function calls the clicked function from the RoleCheckboxController:

App.RoleCheckboxController = Em.Controller.extend({
    clicked: function(evt){
        //Really do the thing
    }
});

But this does not work. How could I fix this ?

JSFiddle: http://jsfiddle.net/3fMpD/


回答1:


You can instantiate and associate the controller to the view using the correct naming conventions.

For example, this would associate the controller to the view:

// Instead of App.RoleCheckBoxController
App.ApplicationController = Ember.Controller.extend( /* ... */ );
App.ApplicationView = Ember.View.extend( /* .. */ );

JSFiddle: http://jsfiddle.net/YL5rQ/




回答2:


@c4p is definitely right and the problem there is that your controller is not being created, and furthermore App.RoleCheckbox has no way of knowing it should use App.RoleCheckboxController as its controller.

I am not quite sure if this is the most Ember-y way of doing this but you can set the controller in the init (constructor function) of the Checkbox view, and then just make sure you send to the controller all the properties it needs to work with:

App.RoleCheckbox = Em.Checkbox.extend({

    init: function(){
      this._super();
      this.set('controller', new App.RoleController());  
    },

    userRolesBinding: 'parentView.user.roles',

    checked: function () {
        var userRoles = this.get('userRoles');
        return userRoles.contains(this.get('content'));
    }.property('content', 'userRoles.@each'),

    click: function (evt) {
        this.get('controller').send('clicked',this.checked, this.content);
     }
});

And the controller's code (just changing the parameters used in the function);

App.RoleCheckboxController = Em.ObjectController.extend({
    clicked: function(checked,role){

         var userRoles = App.User.roles;

         console.log("userRoles = ", userRoles);
         console.log("role = ", role);
         console.log("will be: ", !checked ? "removed" : "added");

         if (checked) { 
             userRoles.pushObject(role); 
         } else {
             userRoles.removeObject(role);
         }

         console.log("updated userRoles = ", userRoles);

    }
});

Working fiddle here: http://jsfiddle.net/cfSwq/3/

Hope this helps!




回答3:


Your App.RoleCheckboxController is never created. The way you have things set up there will only be an instance of ApplicationController.

You can move the logic back into the view's click event to have everything work:

App.RoleCheckbox = Em.Checkbox.extend({
    userRolesBinding: 'parentView.user.roles',

    checked: function () {
        var userRoles = this.get('userRoles');
        return userRoles.contains(this.get('content'));
    }.property('content', 'userRoles.@each'),

    click: function (evt) {
        console.log("event triggered:", evt);
        //var controller = this.get("controller");
        //controller.clicked(evt);
        var isPresent = this.get('checked'),
            userRoles = this.get('userRoles'),
            role = this.get('content');

         console.log("userRoles = ", userRoles);
         console.log("role = ", role);
         console.log("will be: ", isPresent ? "removed" : "added");

         if (!isPresent) { 
             userRoles.pushObject(role); 
         } else {
             userRoles.removeObject(role);
         }
     }
});

Updated JSFiddle



来源:https://stackoverflow.com/questions/16281401/ember-how-to-create-and-bind-a-checkbox-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!