javascript - How to use underscore template tag from a backbone model? -
After
I have a very simple simple time app using json from an API,
mandatory form By leaving everything from calling the template json value inside the template
var time = new couple application (); Time. AddRegions ({Main Regions: "Main Area"}); TimeModel = Backbone.Model.extend ({Error: {Time: "Now", milliseconds_since_epoch: 1234, Date: "Today"}, urlRoot: 'http://date.jsontest.com/'}); Var Tudetime = New Timemodel (); TimeView = Marionette.ItemView.extend ({Template: "#template", Model: TodaysTime, onRender: function () {TodaysTime.fetch ({Success: Function (apiTime) {console.log (apiTime.attributes.time); TodaysTime = API;}}}}}}} Var timeline = new time view (); Time.mainRegion.show (timeView); Time.start (); and then in my template I have this
& lt; p & gt; & lt;% - time%> & lt;% - milliseconds_sense_pok% & gt; & lt; / p & gt; I have not found any errors and nothing is showing, but is located inside console.log (apiTime.attributes.time) function < P> If inside the console I type this code as> TodaysTime.attributes.time also works but if I try the code> & lt <; p & gt; & lt;% - TodaysTime.attributes.time% & gt; & Lt ; / P & gt; I get the default value now What is the proper way to display value from the model?
Github link for
This happens before you bring because you render the model data from the server The reason is that you see the default value you want L'must use promises or listeners on callback. How it can be fixed:
... var current time = new timemodel (); Today's time (time);} TimeView = Marionette.ItemView.extend ({Template: "#template", onRender: function () {console.log ('rendered'); TodaysTime.fetch ({success: function (apiTime) {console.log (apiTime.attributes .Time); } }); } }); Var timeline = new time view ({Model: TodaysTime}); ... Edit after you comment: OK, it really depends on your case. For some examples, this argument is transferred in routes. You can add ItemView to the Start function (but fetch to onRender before) something like this: start: function () {this.model.on ('sync', this.render, this); This.model.fetch ({reset: true, change: true}); This return; }
Comments
Post a Comment