簡體   English   中英

無法獲得控制器屬性和綁定以在Ember中正常工作-拉出我的頭發

[英]Can't get controller properties and bindings to work properly in Ember - pulling my hair out

早上好,

我會很感激您能提供的任何幫助。 我目前正在沮喪地拔頭發。 我真的很想擺脫Ember的煩惱,但我必須缺少一些東西。

我要做的只是以下內容:

  1. 用戶訪問/rooms路線,應用程序執行App.Room.find()調用
  2. 用戶訪問/rooms/1路線,應用在RoomController上設置了selectedRoom屬性。
  3. 使用rooms模板中的selectedRoom屬性指示當前選中的房間。
  4. 使用每個RoomControllerselectedRoom屬性創建一個isSelected計算屬性,以調整該特定房間的顯示

這應該很容易,但是對我來說不起作用。

路線

App.RoomsRoute = Ember.Route.extend
  setupController: ->
    @controllerFor('application').set('currentRoute', 'rooms') 
  model: ->
    App.Room.find()

App.RoomRoute = Ember.Route.extend
  setupController: (model) ->
    @controllerFor('application').set('currentRoute', 'rooms') 
    @controllerFor('rooms').set('selectedRoom', model) 
  model: ->
    App.Room.find(params.room_id)

控制者

(這應該設置一個默認值,以便當用戶僅在/rooms ,有一個字符串可以寫入模板)

App.RoomsController = Ember.ArrayController.extend
  selectedRoom: 'default'

App.RoomController = Ember.ObjectController.extend
  needs: ['rooms']
  isSelected: ( ->
    selectedRoom = 'controllers.rooms.selectedRoom'
    if selectedRoom.id == @get('id')
      return true
    else
      return false
  ).property('controllers.rooms.selectedRoom')

模板

房間

<p>My selected room: {{ selectedRoom.room_id }}</p>

{{#each room in controller}}
  {{#linkTo 'rooms.room' room}} Room {{ room.room_id }} {{/linkTo}}
{{/each}}

房間

# This template is a contrived simplification of what I want to do... but essentially I just want to be able to access the `isSelected` property of the `RoomController`
<div {{ bindAttr class='isSelected' }}>
  <p>Room {{ room.room_name }}</p>
</div>

當我在較舊版本的Ember(v1.0.0-pre.4)上運行您的代碼(填充一些空白)時,它似乎可以按照您的要求工作,單擊時選中的房間會突出顯示。

當我嘗試使用最新版本(1.0.0-rc.4)時,它無法正常工作,因此這告訴我,應該歸咎於Ember的重大變化。

根據此答案 ,將在不同情況下調用modelsetupController掛鈎。

為了使您的代碼正常工作,我必須添加controller.set("model", model); setupController

App.RoomsRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    this.controllerFor('application').set('currentRoute', 'rooms');
    // Load the model property here because the model hook isn't 
    // called in this case
    controller.set("model", model);
  },
  model: function() {
    return App.Room.find();
  }
});`

不是Ember專家,也不確定這是否與您遇到的問題相同,但這對我有用。 我嘗試的完整工作代碼在這里: https : //gist.github.com/jasonschock/5667656#file-app-js-L15

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM