Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery ui 主干jquery ui对话框按钮外部功能_Jquery Ui_Backbone.js_Dialog - Fatal编程技术网

Jquery ui 主干jquery ui对话框按钮外部功能

Jquery ui 主干jquery ui对话框按钮外部功能,jquery-ui,backbone.js,dialog,Jquery Ui,Backbone.js,Dialog,对于渲染函数内的jquery ui对话框,是否可以使用指向另一个函数而不是内联函数的按钮 var MyView = Backbone.View.extend({ submit: function(event) { /* foo */ }, buttons: [{ 'text' : 'SUBMIT', 'click' : this.submit // <== like this }], render: function() { this.$el.a

对于渲染函数内的jquery ui对话框,是否可以使用指向另一个函数而不是内联函数的按钮

var MyView = Backbone.View.extend({
  submit: function(event) { /* foo */ },
  buttons: [{
    'text' : 'SUBMIT',
    'click' : this.submit  // <== like this
  }],

  render: function() {
    this.$el.append("I'm a dialog with a button").dialog({ buttons: this.buttons });
    return this;
  }
});

声明视图时会解释
按钮
数组,此时
将该
设置为根对象(可能是
窗口
)。您可以通过将某些内容分配给
window.submit
来演示此行为。比如说,

window.submit = function() {
    console.log('window submit');
}
当您单击按钮时触发。请参见演示

解决此问题的一个方法是将您的定义用作模板,为每个实例构建自定义按钮数组。大概是这样的:

var MyView = Backbone.View.extend({
    submit: function(event) {
        console.log(this, 'submit');
    },
    buttons: [{
        'text' : 'SUBMIT',
        'click' : 'submit'
    }],

    render: function() {
        var mybuttons;

        //extract the buttons from an array or function,
        mybuttons = _.result(this, 'buttons');

        //build the array
        mybuttons = _.map(mybuttons, function(obj) { 
            // for each object describing a button

            //create a clone to avoid problems
            obj = _.clone(obj); 
            //set the callback, bound to the view
            obj.click = _.bind(this[obj.click], this);  
            return obj;
        }, this);

        this.$el.append("I'm a dialog with a button").dialog({
            buttons: mybuttons
        });
        return this;
    }
});

请参阅玩这个问题的一些背景知识:我按原样运行了上面的代码,引擎似乎找不到提交:未捕获类型错误:无法调用未定义jquery ui.js:10018$.widget的方法“应用”。\u createButtons.$.each.props.click jquery ui.js:10018 jquery.event.dispatch jquery-1.9.1.js:3074 jquery.event.add.elemData.handle jquery-1.9.1.js:2750Cool,谢谢!这样,我甚至可以将贴图调用移动到渲染函数之外!
var MyView = Backbone.View.extend({
    submit: function(event) {
        console.log(this, 'submit');
    },
    buttons: [{
        'text' : 'SUBMIT',
        'click' : 'submit'
    }],

    render: function() {
        var mybuttons;

        //extract the buttons from an array or function,
        mybuttons = _.result(this, 'buttons');

        //build the array
        mybuttons = _.map(mybuttons, function(obj) { 
            // for each object describing a button

            //create a clone to avoid problems
            obj = _.clone(obj); 
            //set the callback, bound to the view
            obj.click = _.bind(this[obj.click], this);  
            return obj;
        }, this);

        this.$el.append("I'm a dialog with a button").dialog({
            buttons: mybuttons
        });
        return this;
    }
});