1

我正在使用骨干网 + jquery mobile,无法通过下划线模板将我的模型(集合)数组输出到 UI。

我的模型如下:

var shortcake = Backbone.Model.extend({

    defaults: function() {
      return {
        name: "No Slot", 
        butter: false, 
        time: "3pm",
        icon: "plus",
        deladd: "ADD"
      };
    },

    initialize: function() {
      this.bind("change:butter", function(){
      if (this.model.butter == false) {
        this.set({name: this.defaults.name});
      };
        }),
    }
});

我的收藏如下:

var shortcakes = Backbone.Collection.extend ({
    model: shortcake
});

var shortcake1 = new shortcake({ name: "How Bizarre", butter: "true", time: "1", icon:"plus", deladd:"ADD" });
var shortcake2 = new shortcake({ name: "Sexual Healing", butter: "true", time: "1", icon:"plus", deladd:"ADD" });
var shortcakeAlbum = new shortcakes([ shortcake1, shortcake2]);

而我的观点:

var shortcakeUI = Backbone.View.extend ({

    tagName: "li",
    template: _.template($('#shortcakeTemplate').html()),


    initialize: function(){
    this.render();
    },

    render: function() {
        var variables = { namee: name };
          if (this.model.butter == false) {
            this.model.deladd = "ADD";
            this.model.icon= "plus";
            this.el.html( template );
          }
          else {
                this.model.deladd = "DELETE";
            this.model.icon= "minus";
            this.el.html( template );
          }
        },

)};


var ShortcakeUI = new shortcakeUI({ 
collection : shortcakeAlbum, 
el: $("#shortcakeinterface")[0] 
});
ShortcakeUI.render();

我的html是:

    <ul data-role="listview" data-split-icon="gear" data-split-theme="d">
        <div id="shortcakeinterface"></div>
    </ul>
<!---Templates--->
            <script id="shortcakeTemplate" type="text/template">
        <% for(var i = 0; i < shortcakeAlbum.length; i++){ %>
        <% var shortcakez = shortcakeAlbum[i]; %>

            <fieldset class="ui-grid-a">
                <div class="ui-block-a">

                    <h3><%= shortcakez.time %></h3>
                    <p><%= shortcakez.name %></p>      

                </div>

                <div class="ui-block-b">

                    <div id="8" data-role="controlgroup" data-type="horizontal" >
                       <a href="#" data-role="button" data-icon="<%= shortcakez.icon %>"><%= shortcakez.deladd %></a>
                       <a href="#" data-role="button" data-icon="plus">Test</a>
                    </div>

                </div>     
            </fieldset>
        <% } %>
        </script>

因此,有了这些,我的 UI 不会显示加载的模型列表。刚开始研究骨干网和js,我在这里做事吗?

4

1 回答 1

0

您需要在 render() 中将模型传递给您的模板。

代替

this.el.html( template );

做这个

$(this.el).html( this.template(this.model.toJSON()) ); 
于 2012-04-29T23:57:34.210 回答