问题
I am trying to access backbone views in sails framework. I fetch data from the server and I am trying to push them to the DOM. Actually, I ve created a model-controller for the tags I store data from mongo to tags model and I return to backbone the url of tags view. I want to display those data to my DOM element. I am trying to find how is it possible to do so in the sails since I am receiving is not defined error. My DOM element code is the following:
51| <div id="profiles" class = 'hashTagsCloud'>
52| <script id="profileTemplate" type="text/template">
>> 53| <%= tagsCloud.tags.join("     ")%>
54| </script>
55| </div>
tagsCloud is not defined
Where tagsCloud is a item of the json file I got from the server. Backbone code for views:
var ProfileView = Backbone.View.extend({
el: "#profiles",
template: _.template($('#profileTemplate').html()),
render: function(eventName) {
_.each(this.model.models, function(profile){
var profileTemplate = this.template(profile.toJSON());
//push data to obj for map script
obj = profile.toJSON();
// Add data to DOM element
$(this.el).html(profileTemplate);
}, this);
return this;
}
});
The above backbone logic works like a charm in apache. However in sails I got not defined error. How can I define, in proper way, a view for tagsCloud item in my index file?? My json file is the following:
[
{
tstamp: 1366626103000,
tagsCloud: {
sort: "asc",
tags: [
"Lorem ipsum dolor sit amet consectetur"
]
},
id: "529da369380eb213e804a673"
}
]
Moreover I add some actions in my homeController file so as to send to ejs file the json data:
index: function (req,res)
{
console.log(req.tags); // tags is the name of the model-controller
res.view({
tags: req.tags
});
},
'home': function (req,res)
{
res.view();
}
Is there anything I ve to change in backbone view code to properly update my index view???
回答1:
Finally I found a way to send fetched data to the DOM from the backbone. I ve changed template code inside backbone code, so as to send directly the data to the DOM element. Here is my code:
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
var TagsView = Backbone.View.extend({
el: "#profiles",
template: _.template("<div><p>{{ tg.tagsCloud.tags }}</p></div>"),
render: function(eventName) {
_.each(this.model.models, function(tags){
var tagsTemplate = this.template(tags.toJSON());
//push data to obj for map script
tg = tags.toJSON();
// Add data to DOM element
$(this.el).html(tagsTemplate);
}, this);
return this;
}
});
来源:https://stackoverflow.com/questions/20354896/backbone-view-in-sails-ejs-file