2014-02-11 41 views
4

我有一個Handlebars模板,它在EmberJS中用作特定路由的模板。該路線提供了這樣一個模型:把手如果在每個內部的聲明丟失上下文

{ 
    "Id": 50, 
    "UserId": 2, 
    "Name": "sdfq", 
    "UserFullName": "Bla bla", 
    "branches": [ 
      {"Id": 2, "Name": "Test branch23", "ProviderId": 50, "AvailabilityId": 1}, 
      {"Id": 3, "Name": "Branch nr 21", "ProviderId": 50, "AvailabilityId": null} 
    ] 
} 

對於這個模型我渲染的模板是這樣的:

<table class="table table-bordered "> 
       <thead> 
        <tr class="active"> 
         <th>{{language 'Id'}}</th> 
         <th>{{language 'Name'}}</th> 
         <th></th> 
        </tr> 
       </thead> 
       {{#each branches}} 
       <tr> 
        <td>{{Id}}</td> 
        <td>{{Name}}</td> 
        <td> 
         {{#link-to 'branch.edit' ProviderId Id tagName='button' class='btn btn-primary' }}<span class="glyphicon glyphicon-pencil"></span> Edit{{/link-to}} 
         {{#if AvailabilityId}} 
          {{#link-to 'availability.edit' AvailabilityId tagName='button' class='btn btn-primary' }}<span class="glyphicon glyphicon-calendar"></span> Availability <span class="glyphicon glyphicon-pencil"></span>{{/link-to}} 
         {{else}} 
          {{#link-to 'availability.add_for_branch' ../Id tagName='button' class='btn btn-success' }}<span class="glyphicon glyphicon-calendar"></span> Availability <span class="glyphicon glyphicon-plus"></span>{{/link-to}} 
         {{/if}} 
        </td> 
       </tr> 
       {{/each}} 
      </table> 

一切都呈現精緻的第一次和正確的鏈接顯示爲正確的行。 現在,當我嘗試點擊一個鏈接availability.edit我得到一個錯誤 未捕獲TypeError:無法讀取未定義的屬性'AvailabilityId' 如果我追查錯誤,那麼它會導致與我的代碼(jQuery內部函數)無關。

Uncaught TypeError: Cannot read property 'AvailabilityId' of undefined ember-1.3.1.js:3936 
ChainNodePrototype.unchain ember-1.3.1.js:3936 
ChainNodePrototype.remove ember-1.3.1.js:3914 
Ember.unwatchPath ember-1.3.1.js:4087 
Ember.unwatch ember-1.3.1.js:4156 
Ember.removeObserver ember-1.3.1.js:5406 
(anonymous function) ember-1.3.1.js:23856 
sendEvent ember-1.3.1.js:2351 
Ember.Evented.Ember.Mixin.create.trigger ember-1.3.1.js:17629 
Ember.CoreView.Ember.Object.extend.trigger ember-1.3.1.js:21769 
superWrapper ember-1.3.1.js:1230 
ViewCollection.trigger ember-1.3.1.js:21834 
Ember.View.Ember.CoreView.extend._notifyWillDestroyElement ember-1.3.1.js:23348 
Ember.merge.destroyElement ember-1.3.1.js:24337 
Ember.View.Ember.CoreView.extend.destroyElement ember-1.3.1.js:23323 
superWrapper ember-1.3.1.js:1230 
Ember.CoreView.Ember.Object.extend.destroy ember-1.3.1.js:21803 
superWrapper ember-1.3.1.js:1230 
Ember.View.Ember.CoreView.extend.destroy ember-1.3.1.js:23657 
superWrapper ember-1.3.1.js:1230 
(anonymous function) ember-1.3.1.js:24782 
sendEvent ember-1.3.1.js:2351 
notifyBeforeObservers ember-1.3.1.js:2723 
propertyWillChange ember-1.3.1.js:2549 
set ember-1.3.1.js:2815 
Ember.trySet ember-1.3.1.js:2884 
(anonymous function) ember-1.3.1.js:6894 
tryable ember-1.3.1.js:2245 
Ember.tryFinally ember-1.3.1.js:1412 
suspendListener ember-1.3.1.js:2248 
Ember._suspendObserver ember-1.3.1.js:5435 
Binding._sync ember-1.3.1.js:6893 
DeferredActionQueues.flush ember-1.3.1.js:5650 
Backburner.end ember-1.3.1.js:5741 
Backburner.run ember-1.3.1.js:5780 
Ember.run ember-1.3.1.js:6181 
Ember.EventDispatcher.Ember.Object.extend._bubbleEvent ember-1.3.1.js:21515 
handleViewEvent ember-1.3.1.js:21459 
Ember.handleErrors ember-1.3.1.js:899 
(anonymous function) ember-1.3.1.js:21450 
jQuery.event.dispatch jquery-1.10.2.js:5095 
jQuery.event.add.elemData.handle jquery-1.10.2.js:4766 

如果我刪除if語句{{#if AvailabilityId}}並一直顯示兩個鏈接,它們工作正常。 我曾嘗試用修改範圍:

{{#link-to 'availability.edit' ../AvailabilityId tagName='button' class='btn btn-primary' }}<span class="glyphicon glyphicon-calendar"></span> Availability <span class="glyphicon glyphicon-pencil"></span>{{/link-to}} 

但它似乎沒有任何效果不大,甚至../../沒有效果(這會導致所有的懷疑if塊,這是可能會導致錯誤)。

歡迎任何提示。

回答

4

大寫屬性通常被認爲是全局屬性,如果你要使用它們,你應該完全限定它們。這很可能是您遇到的問題

{{#each branch in branches}} 
    <tr> 
     <td>{{branch.Id}}</td> 
     <td>{{branch.Name}}</td> 
     <td> 
     {{#link-to 'branch.edit' branch.ProviderId branch.Id tagName='button' class='btn btn-primary' }}<span class="glyphicon glyphicon-pencil"></span> Edit{{/link-to}} 
     {{#if branch.AvailabilityId}} 
      {{#link-to 'availability.edit' branch.AvailabilityId tagName='button' class='btn btn-primary' }}<span class="glyphicon glyphicon-calendar"></span> Availability <span class="glyphicon glyphicon-pencil"></span>{{/link-to}} 
     {{else}} 
      {{#link-to 'availability.add_for_branch' branch.Id tagName='button' class='btn btn-success' }}<span class="glyphicon glyphicon-calendar"></span> Availability <span class="glyphicon glyphicon-plus"></span>{{/link-to}} 
     {{/if}} 
     </td> 
    </tr> 
{{/each}} 
+0

不幸的是,這不是問題的原因。當點擊其中一個鏈接時,我仍然得到相同的錯誤,看起來像點擊鏈接後「分支」未定義。 – Indrek

+0

事實上,如果我單擊其中一個鏈接(availability.edit或availability.add_for_branch)兩次,它第一次引發錯誤,第二次它設法更改正確鏈接的url,但不會渲染模板(也許第一次點擊,這導致了錯誤,使得Ember認爲它已經提供了一個新的視圖,不需要再次執行或smt) – Indrek

+1

是的,它看起來像當它設置手錶它忽略範圍內,最好的辦法是下降到小寫屬性名稱,http://emberjs.jsbin.com/lidud/2/edit – Kingpin2k