2012-03-09 7 views
1

I 다음 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> 

나는이 개 질문을 :

  • 현재 배열의 첫 번째 요소를 사용하는 방법을 모르기 때문에 문자열입니다. 내 생각은 다른 CSS 클래스 배열의 첫 번째 요소를 인쇄하는 것입니다.

  • 둘째, 참여자 배열을 인쇄하는 방법입니다. 참가자 배열에 4 명의 플레이어를 배치하면 각각 4 회 반복됩니다. 하지만 이름이나 이미지와 같은 필드를 인쇄하는 방법을 모르겠습니다.

감사합니다. 내가 여기에 작업 바이올린에 코드를 변환 한

답변

2

: http://jsfiddle.net/KbN47/8/

가장 자연적인 방법은 firstObject을 사용하는 배열의 첫 번째 요소에 바인딩합니다. Ember 0.9.5부터 firstObject 및 lastObject는 성능 영향으로 인해 관찰 할 수 없습니다. Ember 팀은 앞으로이 문제를 해결하기를 희망합니다.

앱에서 배열을 많이 변경하지 않으려면 바이올린에서 제공 한 firstObject의 순진한 재정의 기능을 사용해야합니다.

두 번째 질문에 대해서는 참가자를 추출하여 컨트롤러를 분리하여 명확하게 구분하고 코드에 몇 가지 문제를 수정했습니다.

+0

감사합니다. @ luke-melia. 나는 당신의 코드를 점검 할 것이지만 내가 잘못하고있는 주요한 것은 창조에 관한 것이다. 나는 Object.create ({name : "name"}) 대신 Object.create ("name")과 같은 것을하고 있었다. – Seoman

관련 문제