سؤال

I have redirected from another action controller to one controller. this.get('controllers.searchResult').send('searchDoc', query);

Here I obtain array object using AJAX request

App.SearchResultController = Ember.ArrayController.extend({

serverURL: 'someURL',

actions: {
    searchDoc: function(query) {

        $.ajax({
            type: "GET",
            url: serverURL + request,
            data : 'q=' + query,
            dataType : "JSON",
            context : this, // to increase the scope of 
            statusCode : {
                200 : function(response) {

                    var docs = [];
                    response.docs.forEach(function(doc) {
                        docs.push(App.Doc.create(doc));
                    });

                    // do something here so that
                    // docs get save in the model
                    // and result page get reload
                },
                400 : function() {
                    console.log('status 400 something went wrong');
                }
            }
        });
    }
}
});

I am new for Ember JS. I am willing to store/save/add this docs object in the model and reload my route searchResult.

هل كانت مفيدة؟

المحلول

You should keep a reference to the controller and use it to set the content when you get the results back.

Example:

App.SearchResultController = Ember.ArrayController.extend({
serverURL: 'someURL',

actions: {
    searchDoc: function(query) {
        var self = this; // keep a reference to the controller

        $.ajax({
            type: "GET",
            url: serverURL + request,
            data : 'q=' + query,
            dataType : "JSON",
            statusCode : {
                200 : function(response) {

                    var docs = Ember.A();
                    response.docs.forEach(function(doc) {
                        docs.pushObject(App.Doc.create(doc));
                    });

                    self.set('content', docs); // use the controller reference to set the content
                },
                400 : function() {
                    console.log('status 400 something went wrong');
                }
            }
        });
    }
}
});

I also added the usage of an Ember array in the example. Setting the content should trigger an update of your view.

You can transition to searchResult using the following:

this.get('controllers.searchResult').send('searchDoc', query);
this.transitionToRoute('searchResult');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top