Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新一个模型?_Javascript_Ember.js - Fatal编程技术网

Javascript 更新一个模型?

Javascript 更新一个模型?,javascript,ember.js,Javascript,Ember.js,大家好 编辑:我想强调的是,我找不到有关此解决方案的文档。 App.SearchView = Ember.View.extend({ didInsertElement: function () { this._super(); Ember.run.scheduleOnce('afterRender', this, this.focusSearch); }, focusSearch: function () { $(".sea

大家好

编辑:我想强调的是,我找不到有关此解决方案的文档。

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."]; });
    }
});
我正在使用路由执行到我的服务器的搜索查询。服务器执行所有数据逻辑等操作,并返回与给定关键字匹配的对象列表。我将获取这些结果并将它们提供给模型,这样我就可以使用{{{each}}助手对每个结果进行迭代

我遇到的问题是,当searchText(搜索输入)更改时,模型不希望刷新。我试过几种方法。我不担心创建太多ajax请求,因为我的服务器在2ms内执行搜索查询。这是我现在拥有的

App.SearchView = Ember.View.extend({...
编辑: 谢谢您的回答。

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."]; });
    }
});
App.SearchView=Ember.View.extend({
didInsertElement:函数(){
这个;
Ember.run.scheduleOnce('afterRender',this,this.focusSearch);
},
聚焦搜索:函数(){
$(“.searchInput”).focus().val(this.get(“controller”).get(“searchTextI”);
}
});
App.SearchRoute=Ember.Route.extend({
模型:函数(){
返回此.controllerFor('search').processSearch();
}
});
App.SearchController=Ember.ArrayController.extend({
searchTextI:null,
timeoutid:null,
processid:null,
updateSearch:函数(){
if(this.get('timeoutid')){clearTimeout(this.get('timeoutid'));}
var i=this.get('searchTextI');
var sc=此;
clearTimeout(this.get('processid');
this.controllerFor('index').set('searchText',i);//设置转换时的搜索文本
如果(i.长度<3){
this.set('timeoutid',setTimeout(函数(){
sc.controllerFor('index').set(“transitionfromsearch”,true);
sc.运输路线(“索引”);
}, 1500));
}否则{
var self=这个;
this.set('processid',setTimeout(function()){
self.processSearch().then(函数(结果){
self.set('content',result');
});
}, 1000));
}
}.观察('searchTextI'),
processSearch:函数(){
返回$.getJSON('http://api.*********/搜索',{'token':guestToken,'search_query':this.get('searchTextI')}.then(函数(数据){if(数据==“未找到任何结果”){return[];}否则{return data;}}.fail(函数(){return[“ERROR.];});
}
});

模型
挂钩上放置
观察
不会有任何作用。你应该做你想做的事,说的话

processSearch: function () {
    this.set('content', $.getJSON....);
}

不要观察管线内的任何内容,也不要定义任何计算属性。路线不是适合这些人的地方。除此之外,
模型
不会启动,因为
控制器
未定义

实现您想要的目标的一种方法:

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')
});

UncaughtTypeError:Object#没有方法'addArrayObserver'ember-1.2.0.js:13 UncaughtTypeError:Object#没有方法'removeArrayObserver'
;发生什么事了?请看更新的问题。这个问题还没有解决。此外,我还记录了processSearch()返回的内容,并且它返回了一个承诺。不管怎样,只是没有在thenable上返回任何数据。非常感谢。