Why doesn't my simple Ember.js Handlebars helper work when loading data asynchronously?

StackOverflow https://stackoverflow.com/questions/9061341

سؤال

I have a simple Handlebars helper which simply formats a money value. The helper works property when I test with static data, but not when I load data asynchronously. In other words, {{totalBillable}} will output the expected amount, but {{money totalBillable}} will output zero. But only when the data is loaded via an ajax call. What the heck am I doing wrong?

I've tried to pare the code down as much as possible, and also created a jsfiddle here: http://jsfiddle.net/Gjunkie/wsZXN/2/

This is an Ember application:

App = Ember.Application.create({});

Here's the handlebars helper:

Handlebars.registerHelper("money", function(path) {
  var value = Ember.getPath(this, path);
  return parseFloat(value).toFixed(2);
});

Model:

App.ContractModel = Ember.Object.extend({});

App Controller:

App.appController = Ember.Object.create({
    proprietor: null,
});

Contracts Controller (manages an array of contracts):

App.contractsController = Ember.ArrayController.create({
    content: [],

    totalBillable: function() {
        var arr = this.get("content");
        return arr.reduce(function(v, el){
            return v + el.get("hourlyRate");
        }, 0);
    }.property("content"),

When the proprietor changes, get new contract data with an ajax request. When getting data asynchronously, the handlebars helper does not work.

    proprietorChanged: function() {
        var prop = App.appController.get("proprietor");
        if (prop) {
            $.ajax({
                type: "POST",
                url: '/echo/json/',
                data: {
                    json: "[{\"hourlyRate\":45.0000}]",
                    delay: 1
                },
                success: function(data) {
                    data = data.map(function(item) {
                        return App.ContractModel.create(item);
                    });
                    App.contractsController.set("content", data);
                }
            });
        }
        else {
            this.set("content", []);
        }
    }.observes("App.appController.proprietor")
});

If I use this version instead, then the Handlebars helper works as expected:

    proprietorChanged: function() {
        var prop = App.appController.get("proprietor");
        if (prop) {
            var data = [{
                "hourlyRate": 45.0000}];
            data = data.map(function(item) {
                return App.ContractModel.create(item);
            });
            App.contractsController.set("content", data);
        }
        else {
            this.set("content", []);
        }
    }.observes("App.appController.proprietor")

View:

App.OverviewTabView = Ember.TabPaneView.extend({
    totalBillableBinding: "App.contractsController.totalBillable"
});

Kick things off by setting a proprietor

App.appController.set("proprietor", {
    ID: 1,
    name: "Acme"
});

Template:

<script type="text/x-handlebars">
    {{#view App.OverviewView viewName="overview"}}
        <div class="summary">
           Total Billable: {{totalBillable}}<br/>
           Total Billable: {{money totalBillable}}<br/>
        </div>
    {{/view}}
</script>
هل كانت مفيدة؟

المحلول

when using a helper, handlebars does not emit metamorph tags around your helper call. this way, this part of the template is not re-rendered because there is no binding

to manually bind part of a template to be re-rendered, you can use the bind helper:

<script type="text/x-handlebars">
  {{#view App.OverviewView viewName="overview"}}
    <div class="summary">
       Total Billable: {{totalBillable}}<br/>
       Total Billable: {{#bind totalBillable}}{{money this}}{{/bind}}<br/>
    </div>
  {{/view}}
</script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top