【问题标题】:Backbone view in sails .ejs fileSails .ejs 文件中的主干视图
【发布时间】: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("&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();
}

我需要更改主干视图代码以正确更新我的索引视图吗???

【问题讨论】:

    标签: javascript dom backbone.js view sails.js


    【解决方案1】:

    最后,我找到了一种将获取的数据从主干发送到 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;                   
            }       
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多