user138016
user138016

Reputation:

Populate view with ajax in emberjs?

Is is possible to re-render a view after JSON has loaded?

The current template & view I have won't ever render the variable "current_user"

My template:

<body>
...
<script type="text/x-handlebars">
    {{#view MyApp.LogoutView id="logout-btn"}}}
        <button>
            Logout: {{ current_user }}
        </button>
    {{/view}}
</script>
...
</body>

My document ready:

window.MyApp = Ember.Application.create();

$(document).ready(function() {
    MyApp.LogoutView = Ember.View.extend({
        userString: function() {
            var name = this.get('current_user');
            return name;
        }.property('current_user')
    });

    $.getJSON('/api/1/me', function(data) {
        var user = MyApp.UserModel.create({id: data.id, username: data.username});
        MyApp.current_user = user;
    });
});

I also tried: {{ MyApp.current_user }} in the template, but with Firefox it throws error:

Must use Ember.set() to access this property.

On Chrome I get with it:

<button>Logout <MyApp.UserModel:ember161></button>

Thanks.

Upvotes: 1

Views: 1016

Answers (1)

user138016
user138016

Reputation:

I accidentally found a solution, not sure if its a good one, but seems to work:

MyApp.current_user = MyApp.UserModel.create();

$.getJSON('/api/1/me', function(data) {
    var user = MyApp.UserModel.create({id: data.id, username: data.username});
    MyApp.set('current_user', user);
});

Upvotes: 1

Related Questions