backbone.jsHow do I set a model value in Backbone.js?
Backbone.js provides a set() method to set model values. The set() method takes two arguments, the first argument is the attribute name and the second argument is the value. The set() method will set the attribute on the model and fire a "change" event.
Example
var model = new Backbone.Model({
name: 'John Doe'
});
model.set('name', 'Jane Doe');
console.log(model.get('name'));
Output example
Jane Doe
The set() method can also take an object as an argument, in which case it will set multiple attributes on the model.
Example
var model = new Backbone.Model({
name: 'John Doe',
age: 25
});
model.set({
name: 'Jane Doe',
age: 30
});
console.log(model.get('name'));
console.log(model.get('age'));
Output example
Jane Doe
30
The set() method can also take an option as the third argument. The "silent" option will prevent the "change" event from being fired.
Example
var model = new Backbone.Model({
name: 'John Doe'
});
model.on('change', function() {
console.log('model changed');
});
model.set('name', 'Jane Doe', { silent: true });
Output example
None
More of Backbone.js
- How can I decide between using Backbone.js or React.js for my software development project?
- How do I use a template engine with Backbone.js?
- How do I use W3Schools to learn Backbone.js?
- How do I create tabs using Backbone.js?
- How can I create a WordPress website using Backbone.js?
- How can I use Backbone.js to create a Zabbix monitoring system?
- How do I use backbone.js to zip a file?
- ¿Cuáles son las ventajas y desventajas de usar Backbone.js para el desarrollo de software?
- How can I use Backbone.js to wait for a fetch request to complete?
- How can I use Backbone.js with React to build a web application?
See more codes...