sails.ejs文件中的主干视图

Backbone view in sails .ejs file

本文关键字:视图 ejs 文件 sails      更新时间:2024-02-05

我正在尝试访问sails框架中的主干视图。我从服务器获取数据,并试图将它们推送到DOM。实际上,我已经为标签创建了一个模型控制器,我将数据从mongo存储到标签模型,并将标签视图的url返回到主干。我想将这些数据显示到我的DOM元素中。我正在努力寻找如何可能做到这一点,因为我收到的是未定义的错误。我的DOM元素代码如下:

          51| <div id="profiles" class = 'hashTagsCloud'>
          52| <script id="profileTemplate" type="text/template">
       >> 53| <%= tagsCloud.tags.join("&nbsp &nbsp &nbsp")%>
          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();
}

为了正确更新索引视图,主干视图代码中有什么需要更改的吗???

最后,我找到了一种从主干向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());
        //push data to obj for map script
        tg = tags.toJSON();
        // Add data to DOM element
        $(this.el).html(tagsTemplate);
        }, this);
            return this;                   
        }       
        });