Question

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. 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

HTML

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

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'
        });
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top