【发布时间】:2016-05-10 11:00:00
【问题描述】:
我正在自学 Ember-cli,并尝试构建一个简单的应用程序,当用户搜索一个词时它会获取结果。我得到了 ajax 结果就好了,但我不确定如何将它“传递”给模板
这是我现在的 hbs 代码 /app/templates/product-search.hbs:
搜索栏工作得很好。
<p>*Is 'xyz' by deafult. </p>
{{input class="form-control" id="search-string" type="text" value=this.search-string placeholder="Search by Product"}}
<button type="button" {{action "searchByProduct" this.search-string}}>Lookup Product</button>
<ul>
{{#each result as |item|}}
<li>Product Name: {{item.prodName}}</li>
{{else}}
Sorry, nobody is here.
{{/each}}
</ul>
这是路由文件:/app/routes/product-search.js
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
searchByProduct: function(qString) {
var term = qString || 'xyz';
var fetchURL = 'http://some-api.com/search?callback=JSON_CALLBACK&limit=10&term='+term;
var result = Ember.Object.create({
});
return $.ajax({
url: fetchURL,
dataType: 'jsonp'
}).then(function (response) {
result.set('content', response.results);
result = result.get('content');
console.log("Search Result In Then Promise of Model: ");
console.log(result); //I get an array of objects (screenshot included)
return result;
});
}
}
});
控制台操作:
单个对象如下所示:
0: Object
productId: 471744
productName: "xyz"
productViewUrl: "https://someapi.com/us/product/xyz/id471744?uo=4"
imageUrl30: "http://.../source/30x30bb.jpg"
imageUrl60: "http://.../source/60x60bb.jpg"
imageUrl100: "http://.../source/100x100bb.jpg"
collectionId: 700049951
collectionName: "XYZ Collection"
collectionPrice: 9.99
country: "USA"
currency: "USD"
wrapperType: "track"
......
__proto__: Object
所以我确实在我的 result 对象中看到了结果,但我仍然无法在我的模板中显示它。也许没有循环正确的对象?还是我需要传递其他东西?我不知道缺少什么。感谢您提供任何帮助,我已经坚持了几个小时了。
【问题讨论】:
标签: javascript ajax ember.js jsonp ember-cli