正在获取模型中空对象的数组索引

Getting array index of empty objects in model

本文关键字:对象 数组 索引 获取 模型      更新时间:2023-09-26

我是Ember.js和js的新手。我正试着玩动作。。。更具体地说,我的IndexRoute中的空对象在模板上表示为div元素,我对div有一个调用clickedSquare的操作。如何调用模板中单击的任何给定对象的数组索引?我想我缺少了对路线和控制器如何交互的一些基本理解。谢谢你抽出时间。

App = Em.Application.create();

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return [{}, {}, {}, {}, {}, {}, {}, {}, {}];
  }
});
App.IndexController = Em.ArrayController.extend({
    actions: {
        clickedSquare: function() {
            alert('You clicked square ' + ???);
        }
    }
});

这可能是您想要的:http://jsbin.com/kelevega/5/edit

示例中显示了两种方法,一种是使用_view.contentIndex,另一种是通过使用indexOf函数。