简体   繁体   English

如何在骨干JS事件中使用多个参数

[英]How to use multiple parameters in backboneJS events

I need to pass the second parameter of the onchange function (in this case 'border-radius') that I passed on the range input, this value needs to be passed into the console.log on the javascript. 我需要传递在范围输入中传递的onchange函数的第二个参数(在本例中为“ border-radius”),此值需要传递到javascript的console.log中。 It already works for the 'value' variable. 它已经适用于“值”变量。 The 'value'variable returns an integer as it should, but the cssAttribute variable returns 'Object' instead of the 'border-radius' string 'value'变量返回应有的整数,但是cssAttribute变量返回'Object'而不是'border-radius'字符串

HTML HTML

  (...)
  <input type="range" min="0" max="200" onchange="sidebar.setElementAttributes(this.value,'border-radius')">
  (...)

JS: JS:

 var Sidebar = Backbone.Model.extend({
        setElementAttributes: function(value,cssAttribute)
        {
           this.set({ property: value });
               console.log(cssAttribute); //<- works!
        }
        });

        window.sidebar = new Sidebar;   

        sidebar.on('change:property',function(model,value,cssAttribute)
        {
            console.log(value); // <- works!
            console.log(cssAttribute); //<- doesn't work, value is 'Object' instead border-radius'
        });

You can pass values from trigger to listener using option argument (2nd) of trigger. 您可以使用触发器的选项参数(第二个)将值从触发器传递给侦听器。

It'll be sent to your listener as third argument. 它将作为第三个参数发送给您的听众。

Code : 代码:

var Sidebar = Backbone.Model.extend({
    setElementAttributes: function (value, cssAttribute) {
        this.set({
            property: value
        }, {
            cssAttribute: cssAttribute
        });
    }
});

window.sidebar = new Sidebar;

sidebar.on('change:property', function (model, value, options) {
    console.log(options.cssAttribute);
});

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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