Ember.js Emberjs-空模型以更快地加载
是否可以从模型返回一个空对象以更快地加载html?因为我知道页面正在等待模型的设置,所以我想知道是否有人已经尝试过这样做Ember.js Emberjs-空模型以更快地加载,ember.js,Ember.js,是否可以从模型返回一个空对象以更快地加载html?因为我知道页面正在等待模型的设置,所以我想知道是否有人已经尝试过这样做 App.ApplicationRoute = Ember.Route.extend({ model: function() { return; }, setupController: function(controller, model) { controller.set('model', this.store.fin
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return;
},
setupController: function(controller, model) {
controller.set('model', this.store.find('user'));
}
})
当然这不管用。。。但是它应该更新模型没有
[编辑]
我的路线:
App.ProductData = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin)
App.ApplicationRoute = Ember.Route.extend({
setupController: function( controller, model) {
this._super(controller, this.getProductData());
},
getProductData: function() {
var that = this;
var promise = $.getJSON('js/product.json').then( function( data ) {
data = data.map(function( item, idx ) {
item.key = idx;
item.product = true;
item.imagesBase64 = [];
return item;
});
var uniqueImageURLs = data.reduce(function( result, item ) {
item.images.forEach( function( img ) {
result[img] = true;
});
return result;
}, {});
var count = Object.keys( uniqueImageURLs).length;
that.setProperties({complete: count});
var saves = data.map(function( item ) {
var device = that.store.createRecord( 'device', item );
return device.save();
});
console.log(data);
return data;
}).fail(function( err ) {
console.log(err);
var list = that.store.findQuery('device', { product: true });
return list;
});// end of getProductData
return App.ProductData.create({
promise: promise
});
},
});
我还设置了控制器:
App.ApplicationController = Ember.ArrayController.extend({
selectedItem: undefined,
actions: {
selectDevice: function( device ) {
this.set('selectedItem', device);
}
}
})
我使用它来获取用户单击的项目。是的,当然可以,老实说,我看到的唯一问题是,如果您不定义控制器,Ember将为您构建默认控制器。如果Ember必须根据未定义的模型猜出要使用哪个控制器,它将为您提供一个类似于
App.IndexController = Em.Controller.extend()
上面的控制器不会将对它的get/set调用代理给它上面的模型/集合集,但是如果您要定义控制器类型,Ember应该使用它就可以了
App.IndexController = Em.ArrayController.extend();
只要控制器下没有逻辑/路由依赖于要以任何同步方式填充的模型,这是一个完美的实践
下面是一个使用代理承诺的快速示例:
下面是一个使用阵列代理承诺的示例:
我不想加载该页面。我试着跟着youre JsBin,但当我这么做时:
this.\u super(controller,this.getProductData())代码> UI停留在空白页上。我已经编辑了这个问题,向您展示了我的getProductData函数。你能告诉我我是否做错了什么吗?在你的例子中,你是在返回一个承诺,而承诺不是数组/对象。您将希望使用承诺感知对象/数组作为响应。我理解,但我如何才能做到这一点?(我是新来的,我必须承认……)我举了两个例子,一个是对象,第二个是数组。我把它们从我做的一个关于承诺的演讲中拉出来,如果它们有点脱离上下文,那么很抱歉。阿利路亚兄弟!它在工作!:)非常感谢你!现在我将能够有一个更快的网站呵呵再次感谢!