Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computed properties in Backbone

I have a scenario where the data being manipulated on the client is presented and interacted with in a different way than it is represented on the server.

Consider the following event resource returned from the server.

{
  "id": 123,
  "start_at": 1331336004906,
  "end_at": 1331337704906
}

And the following template for editing:

<form>
  <!-- Notice how date and time are separated in the interface -->
  <input type="text" name="start_date" value="{{start_date}}" />
  <input type="text" name="start_time" value="{{start_time}}" />

  <!-- Instead of asking for an end date/time, we ask for the duration -->
  <input type="text" name="duration" value="{{duration}}" />

  <input type="submit" />
</form>

How would I go about treating start_date, start_time, and duration as attributes in my Backbone model without sending them to the server? Am I supposed to modify .toJSON()?

like image 413
bloudermilk Avatar asked Mar 09 '12 23:03

bloudermilk


People also ask

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.

What is backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

What is a model backbone?

Model. Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.

Which is considered the backbone of any HTML document?

js (aka Backbone) is designed to add structure to client-side applications, to avoid a spaghetti-code mess. In the words of Backbone. js: Backbone.


2 Answers

I am using a combination of the initialize() function together with change event listeners to update derived attributes. The idea is first to compute the attributes on model initialization, and second to let the model listen to its own changes and update the attributes accordingly.

My solution looks roughly like this:

MyModel: Backbone.Model.extend({
    initialize: function() {
        this.updateDerivedAttributes();
        this.on('change:start_at', this.updateDerivedAttributes, this);
    },
    updateDerivedAttributes: function() {
        this.set({
            start_date: Utils.dateFromDate( this.get( "start_at" ) ),
            start_time: Utils.timeFromDate( this.get( "start_at" ) ),
            duration: Utils.youGetTheIdea()
        }, {silent:true});
    }
});
like image 129
bes Avatar answered Oct 12 '22 07:10

bes


We are very used to send model.toJSON() to feed the template. And this method is very tricky to overwrite because is used by other components.

But we can feed the template with a custom model.toJSONDecorated() method that can look like this:

# in your Backbone.Model
toJSONDecorated: function(){
  return 
    _.extend( 
      this.toJSON(), 
      {
        start_date : Utils.dateFromDate( this.get( "start_at" ) ),
        start_time : Utils.timeFromDate( this.get( "start_at" ) ),
        duration   : Utils.youGetTheIdea( :) )
      } 
    );
}

Of course this is breaking a few patterns, I can live with it, if you don't you can move this logic to a Decorator class as people have suggested in other answers.

like image 21
fguillen Avatar answered Oct 12 '22 06:10

fguillen