bernie2436
bernie2436

Reputation: 23941

In backbone, do you define a model's attributes before instantiating?

I am learning backbone.js. In various tutorials they define particular backbone models by extending Backbone.Model

For instance:

Person = Backbone.Model.extend({
    initialize: function(){
        alert("Welcome to this world");
    }
});

I am a little confused about a basic point: in backbone, do you define the attributes of the model ahead of time? For instance, can you define a person model that can only have two fields: name and age?

Based on my experience creating Java or C# classes, I would expect that you would define the attributes associated with a model in much the same way that you define the fields associated with an object. But this does not seem to be how backbone.js works. It seems like you define the attributes associated with a model by setting those attributes during instantiation. For instance, you create age and name fields in a person model by instantiating an instance and setting those fields. var person = new Person({ name: "Thomas", age: 67}); Correct?

Relatedly, can you ever do this with a javascript object? I know javascript is interpreted and not compiled so there is no compile-time checking of what fields in a class are getting set/accessed -- but I would think a run-time check would be possible.

Upvotes: 1

Views: 717

Answers (2)

Andy Novocin
Andy Novocin

Reputation: 464

Backbone encourages the use of its setter and getter functions .set and .get. There are some benefits to doing it in this way, every time the setter is used if an attribute changes an event is fired on the model. In this way views (or other models or whatever) can listen to a model and react to attributes changing.

   var Car = Backbone.Model.extend({});

   var myCar = new Car({tires:0});

   myCar.listenTo(myCar, 'change:tires', function(){
       console.log('Now with '+myCar.get('tires'));
   }, this);

   myCar.set({tires: 4}); //logs "Now with 4"

To access those attributes directly (not recommended) you can access the model's attributes object. You can, of course, still set your own attributes outside of this method by just using model.myAttribute = myValue or some such thing but this does NOT fire an event.

  myCar.tires = 3;
  myCar.get('tires'); // still 4

Backbone attributes are instantiated at several distinct times. The primary method is the .set method, which takes a JSON object, iterates over the keys and saves the values. This function is used on instantiation of a new model, on syncing/fetching with a server, and when you manually call it. To fiddle with the instantiation values just add code to the initialize method, to fiddle with server responses add code to a model's parse method.

If you want to set default values for certain properties you can do so by defining a defaults object to your model. Anyhow, I've enjoyed not having to set the attributes manually so that if I want to adjust my API the changes will pass through nicely. If you want to not allow certain properties you can always delete them from attributes in the initialize method.

Upvotes: 1

kinakuta
kinakuta

Reputation: 9037

You would only define attributes as part of the model's definition if you needed some default values set:

Person = Backbone.Model.extend({
    defaults: {
        "species": "homo sapiens"
    },

    initialize: function () {
        alert("Welcome to this world");
    }
});

Beyond that, the attributes will simply be the properties of whatever object you put into the model. So here's an example:

var examplePerson =  new Person({ firstName: "Johnny", lastName: "Smith", age: 30 });

Now firstName, lastName, age, and species are the attributes of your model instance. If you want to do validation on the values being set, provide an implementation for Backbone's validate method.

Upvotes: 3

Related Questions