Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between attributes and toJSON methods in Backbone

Tags:

backbone.js

Is always equivalent use one or the other?

These prints in console same things

class Model extends Backbone.Model
  defaults:
    some: 'thing'
    other: 'item'

model = new Model

console.log model.attributes
console.log model.toJSON()
like image 283
sites Avatar asked May 10 '13 19:05

sites


People also ask

What is module in Backbone JS?

js respectively. The rest of your application code should be divided into modules that can live under their own modules directory. A module is an encapsulated group of structures (for the purposes of our post, Backbone structures) that work cohesively to provide a subset of functionality in your application.

What is El property of backbone JS view?

The Backbone. js View el method defines the element that is used as the view reference. this. el is created from the view's tagName, className, id and attributes properties, if specified.

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.


1 Answers

toJSON() is a standard method that the JavaScript JSON serializer looks for when serializing an object.

In the context of Backbone, if you override toJSON in your model you can change the format of values which get sent to the server when saving. For example you could filter out read only fields like time stamps.

attributes is the JavaScript object containing the model data, that's what gets altered when you use model.set(). Except if you don't use set() to alter values, then you bypass all the events and loose some of the benefits of backbone. So only use attributes directly if you know what you're doing.

like image 148
Ventajou Avatar answered Nov 06 '22 06:11

Ventajou