因此,我从我继承的项目中得到了这个。

  • 数据的JSON字段。

  •     defaults:{
            "coolstuff":{uuid: null},
                "coolStartDate":new Date(),
                "coolEndDate": new Date(),
                "cooldata":'',
    
                "supercool":'', // I am adding this (trying)
             },
    

    其他一些相关的JS作为:
      offerStart: function() {
         var date = this.get('coolStartDate')
         return (_.isNull(date)) ? new Date() : helper.formatDate(new Date(date)) ;
      },
    

    找到了一些其他数据,并将其作为模板在标记内调用;
    <%= cooldata %>
    

    我在尝试获取“supercool”数据时都失败了。我尝试了不同的Synatax,在页面上,页面外,所有内容。

    我想知道我在 Backbone 中要做的事情(显然我对 Backbone 是新来的)

    为了通过JSON使用我的新数据或数据字段“supercool”,并将其用作页面中的模板。

    在这种特殊情况下;下拉菜单。
     <div class="form-group">
         <select class="form-control filter">
             <option><%= supercool %></option>
             <option><%= supercool %></option>
         </select>
     </div>
    

    更新!

    这是我第一次使用Backbone.js进行的当前尝试,但仍然失败。

    (1.)模型。 (models / page.js)
    define([
        'jquery',
        'underscore',
        'underscore',  // Page > Model
        'backbone',
        'helpers/helpers',
        'bbvalidation'
    ], function(_, Backbone, Helpers) {
        var helper = new Helpers();
        var offerModel = Backbone.Model.extend({
            urlRoot: "/loyalty/api/supercoolfile",
            idAttribute: 'uuid',
            url: function() {
                return this.urlRoot + '/coolguys/' + this.get("id"); //
            },
                defaults:{
             "supercool": "", // here
            },
    

    (2.)查看。 ( View /仪表板/page.js)
    define([
      'jquery',
      'underscore', // Views -- js/views/page.js
      'backbone',
      'vm',
      'text!templates/dashboard/page.html'
    ],
    
      template = _.template(<'<p>Name: <%= supercool %> </p>'),
    
      render: function() {
          this.$el.html( this.template( this.model.toJSON() ) );
          return this;
              }
    
          });
      });
    

    (3.)将数据放入模板(尝试) /dashboard/page.html
             <option><%= supercool %></option>
    

    应该工作吧?他们不适合我。

    最佳答案

    很难确切说明发生了什么,因为我不确定所有代码的连接方式,但是如果您具有如下所示的render函数,它将为您带来“超酷”效果:

    template = _.template(<your template here>),
    
    render: function() {
        this.$el.html( this.template( this.model.toJSON() ) );
        return this;
    }
    

    假设代码的第一位来自传递给您 View 的模型。模板将选择所需的数据。

    编辑:

    单个帖子有很多内容。 Backbone 项目不需要收集。它的目的是持有模型的集合。即您要存储多个要约,而要约的方式是集合。集合将使您能够遍历 View 中的每个模型以呈现它们。另外,您将模型初始化为offerModel,但将OfferModel作为集合中的模型传递。而且我不明白为什么您要重写集合获取方法。

    我不确定确切的方向,但看起来好像有很多不必要的复杂性。

    第二次编辑:

    我创建了一个非常基本的jsfiddle,向您展示了如何在不使用大多数无关信息的情况下呈现此内容:

    http://jsfiddle.net/mmerkes/RdbXH/1/

    关于javascript - 将数据从JSON传递到主干>模板页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19285821/

    10-09 18:16
    查看更多