1
我有以下ember.js代碼:印刷Ember.js模型把手
app.js:
Game = Ember.Application.create({
ready: function() {
Game.gameController.getChallenge();
Game.gameController.participants.pushObject(Game.participants("player1","player1.jpg"));
}
});
Game.challenge = Ember.Object.extend({
challengetype: null,
description: null,
id: null
});
Game.participants = Ember.Object.extend({
name: null,
image: null
});
Game.gameController = Ember.ArrayController.create({
content: [],
current: "",
participants: [],
getChallenge: function() {
var self = this;
var url = "http://api:9393/challenge";
$.getJSON(url, function(data) {
self.insertAt(0,Game.challenge.create(data.challenge));
self.set('current', data.challenge.description);
});
},
});
Game.NewChallengeView = Ember.View.extend({
click: function(evt) {
Game.gameController.getChallenge();
}
});
和模板:
<div id="participants">
{{#each Game.gameController.participants}}
{{name}}
{{/each}}
</div>
current challenge:
<div class="tooltips-gray">
{{Game.gameController.current}}
</div>
我有2個問題:
我正在使用當前的a是一個字符串,因爲我不知道如何使用獲取數組的第一個元素。我的想法是用不同的CSS類打印數組的第一個元素。
第二個是如何打印參與者數組。如果我將4名選手放在參加者陣列上,則每個選手進行4次迭代。但我不知道如何打印名稱或圖像等字段。
謝謝。
謝謝@ luke-melia。我會檢查你的代碼,但我做錯的主要原因是創建。我正在做類似於:Object.create(「name」)而不是Object.create({name:「name」})。 – Seoman 2012-03-11 04:59:13