Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot get the cid of the model while rendering a backbone collection over a template

I am trying to render a backbone collection on a template that is built with mustache.js . The problem is I couldn't get the cid of the model in the template. My code is

        <div class="phone span4">
            <h5> Phone Appointments</h5>
            {{ _.each(slots, function(slot) { }}
                {{ if(slot.aptType == "P"){ }}
                    <h6 cid="{{=slot.cid}}"  aptId="{{=slot.aptId}}"> {{=slot.beginTime}}  - {{=slot.endTime}} </h6>
                {{  }   }}
            {{  }); }}
        </div>

from the above code, I can get the aptId, beginTime and end Time, but not the Cid. How to get the Cid of the model from a collection while rendering it on a template?

and my render method from the view looks like this

    render:function(){
    var template = _.template($("#slot-display-template").html());
    compiledTmp = template({slots: this.collection.toJSON()})
    this.$el.append(compiledTmp);
    }

Also is there any disadvantage of using cid as the unique identifier of a model ?

Thanks in advance !!!

like image 352
digToGetWater Avatar asked Mar 08 '13 16:03

digToGetWater


2 Answers

The cid is not included by default in the toJSON output. You will need to override toJSON in your model definition and include cid.

toJSON: function() {
  var json = Backbone.Model.prototype.toJSON.apply(this, arguments);
  json.cid = this.cid;
  return json;
}
like image 92
Ben Avatar answered Sep 25 '22 03:09

Ben


If you need an ad hock solution, This would work also:

var params = _.extend({}, this.model.toJSON(), {cid: this.model.cid})
like image 35
Alex Shilman Avatar answered Sep 26 '22 03:09

Alex Shilman