0

バックボーン.jsを使用します- this.template.clone();これは私が思う各ループを参照しているため、機能していません。テンプレートを参照するように書くにはどうすればよいですか?ありがとう

model = new Backbone.Model({

    data: [

        { text: "site 1", href: "link1.htm" },
        { text: "site 2", href: "link2.htm" },
        { text: "site 3", href: "link3.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },
        { text: "site 4", href: "link4.htm" },

    ]
});

var View = Backbone.View.extend({

    initialize: function () {
        //console.log('initializing ' + this.options.blankOption);
        this.template = $('#list-template').children();
    },
    el: '#container',
    events: {
        'click button' : 'render'
    },
    model:model,
    render: function(event){
        event.preventDefault();
        var data = this.model.get('data');

        $.each(data,function (i,v) {
            //console.log(v.text + " " + v.href);
            //var li = this.template.clone().find('a').attr('href', v.href).text(v.t.text);
            var li = this.template.clone(); /// this refers to the each loop
        });

    }
});

//var view = new View({blankOption:"emptySTring"});
var view = new View({});
4

2 に答える 2

1

別の変数を使用して、必要な を参照しthisます。

var that = this;

$.each(data,function(i, v) {
    var li = that.template.clone();
});
于 2013-02-25T09:36:59.670 に答える
0

カスタム変数を使用して、関数this内への参照を保持します。render

...
render: function(event){
  var self = this;
  event.preventDefault();
  var data = this.model.get('data');

  $.each(data,function (i,v) {
    //console.log(v.text + " " + v.href);
    //var li = this.template.clone().find('a').attr('href', v.href).text(v.t.text);
    var li = self.template.clone(); /// this refers to the each loop
  });
}
...

JS のスコープがどのように機能するかを調べます。

于 2013-02-25T09:37:36.797 に答える