Handles If an operator inside each loses context

I have a Handlebars template that is used internally by EmberJS as a template for a specific route. The route provides the following model:

{
    "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}
    ]
}

For this model, I create this template:

<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>

Everything displays a penalty for the first time, and the correct links are displayed for the correct lines. Now, when I try to click the link availability.edit, I get an Uncaught TypeError error message: Unable to read the 'AvailabilityId' property from undefined If I track the error, this leads to nothing related to my code (jQuery internal function).

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 I delete the if statement {{#if AvailabilityId}}and display two links all the time, they work fine. I tried to change the scope using:

{{#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
1

, , . ,

{{#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}}
+4

Source: https://habr.com/ru/post/1526415/


All Articles