Javascript 主干-JSON的模型数组

Javascript 主干-JSON的模型数组,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我知道有很多方法可以做到这一点。。。但为什么这种方法不起作用 var selected = this.collection.where({checked: true}); var mapped = _.map(selected, Backbone.Model.prototype.toJSON.call); this.$el.html(this.template(mapped)); 错误为未捕获类型错误:未定义不是函数 有没有更简洁的方法 编辑这是我想要的 var mapped = _.in

我知道有很多方法可以做到这一点。。。但为什么这种方法不起作用

var selected = this.collection.where({checked: true});

var mapped = _.map(selected, Backbone.Model.prototype.toJSON.call);

this.$el.html(this.template(mapped));

错误为未捕获类型错误:未定义不是函数

有没有更简洁的方法

编辑这是我想要的

var mapped = _.invoke(selected, Backbone.Model.prototype.toJSON);
编辑2@nikoshr让它变得更好

var mapped = _.invoke(selected, 'toJSON');

检查映射是否符合预期,在chrome调试器中使用断点或添加console.log(映射)。如果“映射”看起来正常,则可能没有向模板传递某些值。 如果您还有其他问题,请发布模型、集合和模板的定义。
顺便说一句,this.template是一个函数,请再次使用调试器或console.log(typeof this.template)

检查映射是否是您所期望的,在chrome调试器中使用断点或添加console.log(mapped)。如果“映射”看起来正常,则可能没有向模板传递某些值。 如果您还有其他问题,请发布模型、集合和模板的定义。 顺便说一句,this.template是一个函数,请再次使用调试器或console.log(typeof this.template)查看源代码,您将看到,从版本1.5.1开始

_.map = _.collect = function(obj, iterator, context) {
    var results = [];
    if (obj == null) return results;
    if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
    each(obj, function(value, index, list) {
      results.push(iterator.call(context, value, index, list));
    });
    return results;
};
这意味着对于选中的
中的每个对象,您将以
主干.Model.prototype.toJSON.call.call和一个未指定的上下文结束,这可能不是您想要的

在每个模型上调用
toJSON
的最简单方法是使用:

调用调用(列表、方法名、[*参数])
调用名为 按列表中每个值的methodName

适用于您的情况:

var mapped = _.invoke(selected, 'toJSON');
还有一个演示

var c=新主干。集合([
{id:1,选中:true},
{id:2,选中:false},
{id:3,选中:true}
]);
var selected=c.where({checked:true});
var-mapped=u2;.invoke(选中“toJSON”);
console.log(映射)

查看源代码,您将看到,从版本1.5.1开始

_.map = _.collect = function(obj, iterator, context) {
    var results = [];
    if (obj == null) return results;
    if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
    each(obj, function(value, index, list) {
      results.push(iterator.call(context, value, index, list));
    });
    return results;
};
这意味着对于选中的
中的每个对象,您将以
主干.Model.prototype.toJSON.call.call和一个未指定的上下文结束,这可能不是您想要的

在每个模型上调用
toJSON
的最简单方法是使用:

调用调用(列表、方法名、[*参数])
调用名为 按列表中每个值的methodName

适用于您的情况:

var mapped = _.invoke(selected, 'toJSON');
还有一个演示

var c=新主干。集合([
{id:1,选中:true},
{id:2,选中:false},
{id:3,选中:true}
]);
var selected=c.where({checked:true});
var-mapped=u2;.invoke(选中“toJSON”);
console.log(映射)


需要清楚地说明问题。什么不起作用?未捕获的TypeError:undefined不是一个函数,
TypeError
发生在哪里?它应该有一个行号——三行中的哪一行触发了它?需要清楚地说明问题。什么不起作用?未捕获的TypeError:undefined不是一个函数,
TypeError
发生在哪里?它应该有一个行号-三行中的哪一行触发了它?关于
this.template
-主干中的一个常见习惯用法是
..template(this.template({…}))
其中
this.template
实际上是一些标记。而不是下划线模板,我使用www.socketstream.org,它提供预编译的jade模板,所以我的呈现代码看起来像ss.tmpl['template-name'].render({data}),我想它对于把手或其他选项是不同的
where
this.template
实际上是一些标记。我使用www.socketstream.org而不是下划线模板,它提供预编译的jade模板,因此我的渲染代码看起来像ss.tmpl['template-name'].render({data}),我想对于把手或其他一些选项来说是不同的。