【发布时间】:2013-12-03 15:18:46
【问题描述】:
我正在尝试访问sails 框架中的主干视图。我从服务器获取数据并尝试将它们推送到 DOM。实际上,我已经为将数据从 mongo 存储到标签模型的标签创建了一个模型控制器,并且我将标签视图的 url 返回到主干。我想将这些数据显示到我的 DOM 元素。我试图找到如何在风帆中这样做,因为我收到未定义的错误。我的 DOM 元素代码如下:
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
其中 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());
//push data to obj for map script
obj = profile.toJSON();
// Add data to DOM element
$(this.el).html(profileTemplate);
}, this);
return this;
}
});
上面的主干逻辑就像 apache 中的魅力一样。但是在风帆中我没有定义错误。如何以正确的方式在我的索引文件中定义 tagsCloud 项目的视图? 我的 json 文件如下:
[
{
tstamp: 1366626103000,
tagsCloud: {
sort: "asc",
tags: [
"Lorem ipsum dolor sit amet consectetur"
]
},
id: "529da369380eb213e804a673"
}
]
此外,我在 homeController 文件中添加了一些操作,以便将 json 数据发送到 ejs 文件:
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();
}
我需要更改主干视图代码以正确更新我的索引视图吗???
【问题讨论】:
标签: javascript dom backbone.js view sails.js