0
votes

How do I update a model property once a promise (via Ajax call) has returned? Here is my go at it that is not working. In the docs, it appears observing takes place using Ember.set but the model I am returning is not an Ember model object so I don't think that works.

import Ember from 'ember';

export default Ember.Route.extend({

    model: function() {

        var model = { title: "Lorem Ipsum" };
        var userSubscriptions = [];
        var App = this;

        Ember.$.getJSON('http://localhost:1337/company/usersubscription/active/', parameters, function(userSubscriptions) {

            userSubscriptions.forEach(function(data) {

                var userSubscription = App.store.push('usersubscription', data);
                userSubscriptions.pushObject(userSubscription);

                model.title   = "No Lorem Ipsum";
                model.objects = userSubscriptions;

            });

        });

        return model;

    }

});
1
What's the reason for not using an Ember.Object, what do you try to achieve and what is not working in detail?kunerd
I'm kind of confused as how to go about doing so using Ember CLI.morcutt
You simply can use emberjs.com/api/classes/Ember.Object.html#method_create like this: var model = Ember.Object.create({title: "Lorem Ipsum"});. But it seems that you are fighting a bit against the framework. If you could explain what you try to do more in detail, then maybe I or someone else can help you more.kunerd
@kunerd, I agree. It seems I am fighting it. All I want to do is pass other values from my route to the template. An example of why I would want to do this is to display messages when there are errors, issues, no results, etc.morcutt

1 Answers

1
votes

If you use model = Ember.Object.create({title: 'old Title'}), then you can use model.set('title', 'new Title') to set a new title, that will be updated on the view automatically.

Here is a simple example JSBin: http://emberjs.jsbin.com/xuzevizabe/2/edit

But it seems that you instead want to use ember-data to handle your models.