骨架视图在帆.ejs文件 [英] Backbone view in sails .ejs file
问题描述
51 | < div id =profilesclass ='hashTagsCloud'>
52 | < script id =profileTemplatetype =text / template>
>> 53 | <%= tagsCloud.tags.join(& nbsp& nbsp& nbsp)%>
54 | < /脚本>
55 | < / DIV>
标签Cloud没有定义
其中tagsCloud是一个项目我从服务器得到的json文件。视图的主干代码:
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());
//将数据推送到obj用于映射脚本
obj =配置文件。 toJSON();
//向DOM元素添加数据
$(this.el).html(profileTemplate);
},this);
返回此;
}
});
上面的主干逻辑像apache中的魅力一样。然而在帆中我没有定义错误。我如何以正确的方式定义我的索引文件中的tagsCloud项目的视图?
我的json文件如下:
[
{
tstamp:1366626103000,
tagsCloud:{
sort:asc,
标签:[
Lorem ipsum dolor sit amet consectetur
]
},
id:529da369380eb213e804a673
}
]
此外,我添加了一些操作我的homeController文件,以便向ejs文件发送json数据:
index:function(req,res)
{
console.log(req.tags); //标签是模型控制器的名称
res.view({
标签:req.tags
});
},
'home':function(req,res)
{
res.view();
}
有什么我可以改变骨干视图代码来正确更新我的索引查看???
最后,我发现了一种从骨干网将获取的数据发送到DOM的方法。我修改了骨干代码中的模板代码,以便将数据直接发送到DOM元素。这是我的代码:
_.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());
//将数据推送到地图脚本的obj
tg = tags.toJSON() ;
//向DOM元素添加数据
$(this.el).html(tagsTemplate);
},this);
return this;
}
});
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???
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;
}
});
这篇关于骨架视图在帆.ejs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!