我是 Ember 的新手,从 URL 加载 JSON 时遇到问题。这是我所拥有的:
var App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend();
App.ApplicationView = Ember.View.extend({ templateName: 'application' });
App.OrdersListController = Ember.Controller.extend();
App.OrdersListView = Ember.View.extend({ templateName: 'orders' });
App.Order = Ember.Object.extend();
App.Order.reopenClass({
find: function(){
//not implemented yet
},
findAll: function(){
var content = [];
$.ajax({
url: 'http://api.domain/orders',
dataType: 'jsonp',
success: function(response){
response.data.forEach(function(order){
this.content.addObject(App.Order.create(order))
}, this)
}
});
return this.content;
}
});
App.Router = Ember.Router.extend({
enableLogging: true,
// The initial state for the router, contains every other.
root: Ember.Route.extend({
goOrders: Ember.Route.transitionTo('orders'),
goLocations: Ember.Route.transitionTo('locations'),
goReports: Ember.Route.transitionTo('reports'),
goUsers: Ember.Route.transitionTo('users'),
goHelp: Ember.Route.transitionTo('help'),
//Authenticated routes
index: Ember.Route.extend({
route: '/',
redirectsTo: 'orders'
}),
orders: Ember.Route.extend({
route:'/orders',
initialState: 'index',
//Orders view.
index: Ember.Route.extend({
route: '/',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet({
name:'ordersList',
context: App.Order.findAll()
});
}
})
})
})
});
App.initialize();
在我的模板(简化)中:
<script type="text/x-handlebars" data-template-name="application">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="orders">
{{#each order in controller.content}}
<p>id: {{order.ord_id}}</p>
{{/each}}
</script>
有了这个,我没有得到错误,但什么也没有出现。如果我将他的 JSON 粘贴到 connectOutlet 的上下文中,它仍然可以工作。我的 findAll() 函数有问题吗?