【问题标题】:How can I parse this JSON for use in Backbone View如何解析此 JSON 以在主干视图中使用
【发布时间】:2017-05-22 14:22:37
【问题描述】:

我正在尝试学习 Backbone,但似乎无法将 fetch 函数中的数据匹配到我的 Underscore 模板中。如何在我的 JSON 中获取 children 数组并将其与模板匹配?

Backbone.View 如下所示:

var Projects = Backbone.Collection.extend({
    url: '/tree/projects'
});

var Portfolio = Backbone.View.extend({
    el: '.page',
    render: function () {
        var that = this;
        var projects = new Projects();
        projects.fetch({
            success: function (projects) {
                var template = _.template($('#projects-template').html());
                that.$el.html(template({projects: projects.models}));
            }
        })
    }
});

在网址:http://localhost:3000/portfolio/api/tree/projects

返回的 JSON 如下所示:

{  
   id:"projects",
   url:"http://localhost:8888/portfolio/projects",
   uid:"projects",
   title:"Projects",
   text:"",
   files:[  

   ],
   children:[  
      {  
         id:"projects/example-1",
         url:"http://localhost:8888/portfolio/projects/example-1",
         uid:"example-1",
         title:"Example 1",
         images:"",
         year:"2017",
         tags:"Website",
         files:[  

         ],
         children:[  

         ]
      },
      {  
         id:"projects/example-2",
         url:"http://localhost:8888/portfolio/projects/example-2",
         uid:"example-2",
         title:"Example @"2
         text:"Example 2's text",
         year:"2016",
         tags:"Website",
         files:[  
            {  
               url:"http://localhost:8888/portfolio/content/1-projects/4-example-2/example_ss.png",
               name:"example_ss",
               extension:"png",
               size:244845,
               niceSize:"239.11 kB",
               mime:"image/png",
               type:"image"
            }
         ],
         children:[  

         ]
      },
   ]
}

我的下划线文件如下所示:

<script type="text/template" id="projects-template">
<h4>tester</h4>
<div>
    <% _.each(projects.children, function (project) { %>
    <div>
        <div><%= project.get('year') %></div>
        <div><%= project.get('title') %></div>
        <div><%= project.get('tags') %></div>
    </div>
    <% }); %>
</div>
</script>

【问题讨论】:

    标签: javascript json backbone.js


    【解决方案1】:

    您可以在集合上定义解析方法:

    var Projects = Backbone.Collection.extend({
      url: '/tree/projects',
      parse: function(response){
        /* save other data from response directly to collection if needed.
         for eg this.title = response.title; */
        return response.children; // now models will be populated from children array
      }
    });
    

    【讨论】:

      【解决方案2】:

      不要使用parse

      虽然我通常同意 with TJ,但在集合上使用 parse 更像是一种 hack,而不是一个明确的解决方案。它只会用于获取项目的子项目,仅此而已。

      parse 函数不应该对集合产生副作用,并且使用这种方法,更改和保存父项目上的字段不会轻松。

      它也没有处理它是一个嵌套结构的事实,它不仅仅是一个包装数组。

      这个函数在接收打包数据时效果最好:

      {
          data: [{ /*...*/ }, { /*...*/ }]
      }
      

      模型和集合

      您在这里拥有的是 projects,其中包含嵌套的 projects。一个项目应该是一个模型。你也有文件,所以你应该有一个File 模型。

      获取每个资源并用它创建模型和集合类。但首先,让共享数据不碍事。

      var API_ROOT = 'http://localhost:8888/';
      

      文件

      var FileModel = Backbone.Model.extend({
          defaults: {
              name: "",
              extension: "png",
              size: 0,
              niceSize: "0 kB",
              mime: "image/png",
              type: "image"
          }
      });
      
      var FileCollection = Backbone.Collection.extend({
          model: FileModel
      });
      

      项目

      var ProjectModel = Backbone.Model.extend({
          defaults: function() {
              return {
                  title: "",
                  text: "",
                  files: [],
                  children: []
              };
          },
          getProjects: function() {
              return this.get('children');
          },
          setProjects: function(projectArray, options) {
              return this.set('children', projectArray, options);
          },
      
          getFiles: function() {
              return this.get('files');
          },
          getSubProjectUrl: function() {
              return this.get('url');
          }
      });
      
      var ProjectCollection = Backbone.Collection.extend({
          model: ProjectModel,
          url: API_ROOT + '/tree/projects'
      });
      

      项目视图

      然后,为项目制作视图。这是一个简单的示例,有关优化渲染的提示,请参阅附加信息。

      var ProjectView = Backbone.View.extend({
          template: _.template($('#projects-template').html()),
          initialize: function(options) {
              this.options = _.extend({
                  depth: 0, // default option
              }, options);
      
              // Make a new collection instance with the array when necessary
              this.collection = new ProjectCollection(this.model.getProjects(), {
                  url: this.model.getSubProjectUrl()
              });
          },
      
          render: function() {
              this.$el.html(this.template(this.model.toJSON()));
              this.$projectList = this.$('.list');
      
              // use the depth option to avoid rendering too much projects
              if (this.depth > 0) this.collection.each(this.renderProject, this);
              return this;
          }
      
          renderProject: function(model) {
              this.$projectList.append(new ProjectView({
                  model: model,
                  depth: depth - 1
              }).render().el);
          }
      });
      

      使用这样的模板:

      <script type="text/template" id="projects-template">
          <h4><%= title %></h4>
          <span><%= year %></span><span><%= tags %></span>
          <p><%= text %></p>
          <div class="list"></div>
      </script>
      

      使用视图:

      var model = new ProjectModel({ id: "project" });
      model.fetch({
          success: function() {
              var project = new ProjectView({
                  model: model,
                  depth: 2
              });
          }
      });
      

      附加信息

      【讨论】:

      • 非常感谢 Emilie 提供了如何分解模型的清晰示例
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多