English 中文(简体)
页: 1
原标题:backbone.js collection not responding to .each

我的发言应该非常简单。 我设立了一个新的收集系统,我想通过这一汇编,在网页上添加收集模式。

get_results: function(){
    $.getJson(this.url,function(response){
        this.search_results = new Kitchon.Collections.searchList(response);
        console.log(this.search_results);
        this.search_results.each(this.render_match);
    }
},
render_match: function(model){
    console.log(model)
},

This returns an error

Uncaught TypeError: undefined is not a function

我的收集工作具有普通结构

_byCid: Object
_byId: Object
_onModelEvent: function () { [native code] }
_removeReference: function () { [native code] }
length: 7
models: Array[7]
__proto__: o

I ve tried LOTS of things, but one thing that stuck out was maybe I had to pass this.search_results.models.each(this.render_match); as that is the actual array, but if I do that I get a Uncaught typeError: Object [object Object],[object Object],...

最佳回答

you lose the execution context when callback function for each method is called

使用_.bind (this.render_match, this),在发出回击时,确保render_match 具有适当的背景

并且由于你对<代码>getJson方法的背书功能不作总结,你正在发现错误。 您还必须使用<代码>bind方法。

http://yehudakatz.com http://yehudakatz.com/08/11/underactive-javascript-Function-inking-and-this/。

矫正法应视此法。

get_results: function(){

    $.getJSON(this.url, _.bind(function(response){

        this.search_results = new Kitchon.Collections.searchList(response);
        console.log(this.search_results);
        this.search_results.each(_.bind(this.render_match, this));
    }, this));
},
render_match: function(model){

    console.log(model)
},

虽然从我所看到的情况来看——我假定你在这里所展示的守则是一种模式或收集方法——正在处理这一看法——你应该这样做! 模型和收集只是储存和校对数据,所有提供和控制应用流动都应在航道者的帮助下进行。

问题回答

<代码>$.getJson 页: 1 分类法中的许多方法就是这样,<编码>这一数值即无效。 页: 1

为解决这一问题,在<代码>$.getJson之前添加提及(例如var_this = ;,而不是使用 >。 法典如下:

get_results: function(){
    var _this = this;
    $.getJson(this.url,function(response){
        _this.search_results = new Kitchon.Collections.searchList(response);
        console.log(_this.search_results);
        _this.search_results.each(_this.render_match);
    }
},
render_match: function(model){
    console.log(model)
},

Just taking a stab here (I don t know anything about Backbone.js), but isn t this what you are looking for:

$.each(this.search_results, function(index, value) { 
  alert(index +  :   + value); 
});

Good Luck!





相关问题
Is HashMap in Java collision safe

I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>> . What happens if the ...

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

PLSQL Collections - how to use table of records?

I m new to PL/SQL and I m trying to use a table of records, but I don t know how to use this feature. What is the problem? DECLARE TYPE TIP IS RECORD ( F1 ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...