Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
Ember.js 计算属性回调的返回值未更新模板_Ember.js - Fatal编程技术网

Ember.js 计算属性回调的返回值未更新模板

Ember.js 计算属性回调的返回值未更新模板,ember.js,Ember.js,对不起,标题太长了。这很难用语言表达。 余烬版本:1.2.0 下面是: 我的组件: App.AutocompleteComponent = Ember.Component.extend({ searchResults: function() { var returnValue var service = new google.maps.places.AutocompleteService();

对不起,标题太长了。这很难用语言表达。 余烬版本:1.2.0

下面是:

我的组件:

App.AutocompleteComponent = Ember.Component.extend({
searchResults: function() {
                   var returnValue
                   var service = new google.maps.places.AutocompleteService();
                   service.getPlacePredictions({options},callback);

                   function callback(results){
                      returnValue = results;
                   }


                    return returnValue;
                   }.property('searchText')
我的模板:

{{input type="text" value=searchText placeholder="Search..."}}
<ul >
   {{#each itemResults}}
     <li>{{this.name}}</li>
   {{/each}}
</ul>
{{input type=“text”value=searchText placeholder=“Search…”
    {{{#每个项目结果}
  • {{this.name}}
  • {{/每个}}
当我使用ember chrome调试工具进行调试时,我可以看到组件正确地保存searchResults值。但模板中没有相应地更新它

有什么想法吗

如果不建议使用这种处理/使用计算属性的方法,可以建议其他方法吗


提前谢谢你。

你可能想消除这一点(我不知道是什么选项,它是一个全局var吗?)。模板是itemResults而不是searchResults


感谢kingpin2k的回复

我发现了其他处理返回的方法,这些方法显然不适用于回调,因为计算属性在某种程度上需要“返回”,这使得它在这个用例中不可行

相反,我选择使用观察员

顺便说一下,这段代码是用来处理自动完成的

以下是最终代码:

WebClient.AutocompleteFromComponent = Ember.Component.extend({
 searchTextChanged: Em.observer('searchText',function(){ 

var service = new google.maps.places.AutocompleteService();

            service.getPlacePredictions({
                input: this.get('searchText'),
                types: ['(regions)'],
                componentRestrictions: {
                    country: 'my'
                }
            }, function(predictions, status) {
                //Start callback function
                if (status != google.maps.places.PlacesServiceStatus.OK) {
                    alert(status);
                    return;
                }
                for (var i = 0, prediction; prediction = predictions[i]; i++) {
                    console.log(prediction.description);
                    mapItem = {};
                    mapItem.name = prediction.description;
                    mapItem.type = 'map'
                    mapItem.reference = prediction.reference;
                    itemResults.push(mapItem);


                }
                //console.log(itemResults)
                self.set('itemResults', itemResults)

            });
})
模板代码仍然相同

WebClient.AutocompleteFromComponent = Ember.Component.extend({
 searchTextChanged: Em.observer('searchText',function(){ 

var service = new google.maps.places.AutocompleteService();

            service.getPlacePredictions({
                input: this.get('searchText'),
                types: ['(regions)'],
                componentRestrictions: {
                    country: 'my'
                }
            }, function(predictions, status) {
                //Start callback function
                if (status != google.maps.places.PlacesServiceStatus.OK) {
                    alert(status);
                    return;
                }
                for (var i = 0, prediction; prediction = predictions[i]; i++) {
                    console.log(prediction.description);
                    mapItem = {};
                    mapItem.name = prediction.description;
                    mapItem.type = 'map'
                    mapItem.reference = prediction.reference;
                    itemResults.push(mapItem);


                }
                //console.log(itemResults)
                self.set('itemResults', itemResults)

            });
})