this is my backbone code ,where iam using a underscore template,it is working fine now i am trying to make that template external and load from there
this is my sample backbone code
Backbone Application
<div class="list"></div>
<script id="personTemplate" type="text/template">
<strong><%= name %></strong> (<%= age %>) - <%= occupation %>
</script>
<script type="text/javascript">
var Person = Backbone.Model.extend({
defaults: {
name: 'Guest User',
age: 23,
occupation: 'worker'
}
});
var PersonView = Backbone.View.extend({
tagName: 'li',
template: _.template( $('#personTemplate').html()),
initialize: function(){
this.render();
},
render: function(){
this.$el.html( this.template(this.model.toJSON()));
}
});
var person = new Person; // a person object created...
//person.set('name', 'abhi');
var personView = new PersonView({ model: person });
personView.el // ---->; You can call this method and it will display the view..
$(document.body).html(personView.el);
</script>
</body>
</html>
what is really need is that i will place this template <strong><%= name %></strong> (<%= age %>) - <%= occupation %>
in a external file. i dont know much about its extension,i think it can be a html file and load the template from there ,i researched about it and find some sample code and tried some thing like this
<script type="text/javascript">
_.mixin({templateFromUrl: function (url, data, settings) {
var templateHtml = "";
this.cache = this.cache || {};
if (this.cache[url]) {
templateHtml = this.cache[url];
alert(templateHtml);
} else {
$.ajax({
url: url,
method: "GET",
async: false,
success: function(data) {
templateHtml = data;
alert(templateHtml);
}
});
this.cache[url] = templateHtml;
}
return _.template(templateHtml, data, settings);
}});
var Person = Backbone.Model.extend({
defaults: {
name: 'Guest User',
age: 23,
occupation: 'worker'
}
});
var PersonView = Backbone.View.extend({
tagName: 'li',
template:_.templateFromUrl("templatefile1.html", {"name": "value","age":10}) ,
// template:_.templateFromUrl("templatefile1.html", this.model.toJSON()) ,
//template: _.template( $('#personTemplate').html()),
initialize: function(){
this.render();
},
render: function(){
alert(this.template);
this.$el.html( this.template);
}
});
var person = new Person; // a person object created...
//person.set('name', 'abhi');
var personView = new PersonView({ model: person });
personView.el // ---->; You can call this method and it will display the view..
$(document.body).html(personView.el);
</script>
but when i am calling teemplate like this template:_.templateFromUrl("templatefile1.html", {"name": "value","age":10}) ,
it is working
but when i trying to pass the pass model to that template like this
template:_.templateFromUrl("templatefile1.html", this.model.toJSON())
i am getting exception like
Uncaught TypeError: Cannot read property 'toJSON' of undefined
how can i resolve this issue
$(document.body).html(personView.el);
, looks like you're storing<script>
s inside your<body>
and$('body').html(...)
clears<body>
before adding the new stuff. – mu is too short