EmberJS:刷新模型? [英] EmberJS: Refreshing a model?

查看:101
本文介绍了EmberJS:刷新模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次

编辑:我想强调,我没有找到解决方案的文档。

我正在使用路由对我的服务器执行搜索查询。服务器执行所有数据逻辑等,并返回与给定关键字匹配的对象列表。我正在采取这些结果并将其提供给模型,以便我可以使用{{#each}}帮助器来迭代每个结果。

I am using a route to perform a search query to my server. The server does all the data logic and such and returns a list of objects that match the keywords given. I am taking those results and feeding them to the model so that I can use the {{#each}} helper to iterate over each result.

我遇到的问题是,当搜索输入的searchText(搜索输入)更改时,该模型不会要刷新。我尝试过几件事情我不担心创建太多的ajax请求,因为我的服务器在2ms中执行搜索查询。这是我现在所在的。

The problem I am having is that the model does not want to refresh when the searchText (search input) changes. I've tried several things. I'm not worried about creating too many ajax requests as my server performs the search query in 2ms. Here's what I have now.

App.SearchView = Ember.View.extend({...



编辑:



感谢您的答案。 / strong>

Thank you for the answer.

App.SearchView = Ember.View.extend({
    didInsertElement: function () {
        this._super();
        Ember.run.scheduleOnce('afterRender', this, this.focusSearch);
    },
    focusSearch: function () {
        $(".searchInput").focus().val(this.get("controller").get('searchTextI'));
    }
});

App.SearchRoute = Ember.Route.extend({
    model: function () {
        return this.controllerFor('search').processSearch();
    }
});

App.SearchController = Ember.ArrayController.extend({
    searchTextI: null,
    timeoutid: null,
    processid: null,
    updateSearch: function () {
        if(this.get('timeoutid')) {clearTimeout(this.get('timeoutid')); }
        var i = this.get('searchTextI');
        var sc = this;
        clearTimeout(this.get('processid'));
        this.controllerFor('index').set('searchText', i); //set the search text on transition
        if(i.length < 3) {
            this.set('timeoutid', setTimeout(function () {
                sc.controllerFor('index').set("transitioningFromSearch", true);
                sc.transitionToRoute('index');
            }, 1500));
        } else {
            var self = this;
            this.set('processid', setTimeout(function() {
                self.processSearch().then(function(result) {
                    self.set('content', result);
                });
            }, 1000));
        }
    }.observes('searchTextI'),
    processSearch: function () {
        return $.getJSON('http://api.*********/search', { 'token': guestToken, 'search_query': this.get('searchTextI') }).then(function(data) { if(data == "No Results Found.") { return []; } else { return data; } }).fail(function() { return ["ERROR."]; });
    }
});


推荐答案

不要在路线内观察到任何东西, t定义任何计算属性。路线不是这些的地方。除此之外,模型不会触发,因为 controller 未定义。

Don't observe anything within a route and don't define any computed properties. Routes are not the place for these. Apart from that, the model doesn't fire because controller is undefined.

实现自己想要的一种方式:

One way to achieve what you want:

App.SearchRoute = Ember.Route.extend({
    model: function () {
      this.controllerFor('search').searchQuery();
    }.observes('controller.searchText') //not triggering an ajax request...
});



App.SearchController = Ember.ArrayController.extend({
   searchQuery: function() {
     return $.getJSON('http://api.**************/search', { 'token': guestToken, 'search_query': t }).fail(function() {
       return null; //prevent error substate.
     });
   }

   onSearchTextChange: function() {
     var controller = this;
     this.searchQuery().then(function(result) {
       controller.set('content', result);
     });
   }.observes('searchText')
});

这篇关于EmberJS:刷新模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆