Ember 2 select 依赖
Ember 2 select dependency
我正在尝试在 ember 中的两个 select 框之间创建依赖关系。
我找到了旧版本 ember 的解决方案:
http://jsbin.com/cibonulora/1/edit
但是对于新版本和带有 json+rest 的完整模型,我似乎无法让它工作。
http://jsbin.com/jumiwogugi/4/edit?html,js,console,output
当我 select 复选框中的一个项目时,它应该过滤第二个元素。但什么也没发生。
谢谢
我的代码:
App = Ember.Application.create({});
App.IndexRoute = Ember.Route.extend({
setupController : function(controller, model) {
this._super(controller, model);
controller.set('producttypes', this.store.find('producttype'));
}
});
App.ProducttypesController = Ember.Object.create({
content: null
});
App.ProductsubtypesController = Ember.Object.create({
content: null
});
App.ProductsubtypeController = Ember.ArrayController.extend({
active: function() {
alert(""); }.property("App.selectedProductType.content").cacheable()
});
//model
App.Producttype = DS.Model.extend({
name: DS.attr(),
});
App.Productsubtype = DS.Model.extend({
name: DS.attr(),
});
var producttypes = {
"producttypes":[
{"id":1,"name":"type1"},
{"id":2,"name":"type2"},
{"id":3,"name":"type3"}
]
};
var productsubtypes = {
"productsubtypes":[
{"id":4,"name":"type4","type_id":"1"},
{"id":5,"name":"type5","type_id":"1"},
{"id":6,"name":"type6","type_id":"3"}
]
};
$.mockjax({
url: '/producttypes',
dataType: 'json',
type: 'get',
responseText: producttypes
});
$.mockjax({
url: '/productsubtypes',
dataType: 'json',
type: 'get',
responseText: productsubtypes
});
我找到了解决问题的方法。
控制器:
App.IndexController = Ember.Controller.extend({
selectedProductTypeChanged: function(){
var obj = this.get('selectedProductType');
if (obj != null) {
console.log(typeof obj.id);
var unfiltered=this.get('productsubtypes');
var filtered=this.get('productsubtypes').filterProperty("producttype",obj.id);
this.set('filterproductsubtypes',filtered );
this.set('selectedProductSubType',null);
}
}.observes('selectedProductType')
});
Link 到 jsbin:http://jsbin.com/juvokacuno/3/edit?html,js,output
我正在尝试在 ember 中的两个 select 框之间创建依赖关系。 我找到了旧版本 ember 的解决方案: http://jsbin.com/cibonulora/1/edit
但是对于新版本和带有 json+rest 的完整模型,我似乎无法让它工作。 http://jsbin.com/jumiwogugi/4/edit?html,js,console,output
当我 select 复选框中的一个项目时,它应该过滤第二个元素。但什么也没发生。
谢谢
我的代码:
App = Ember.Application.create({});
App.IndexRoute = Ember.Route.extend({
setupController : function(controller, model) {
this._super(controller, model);
controller.set('producttypes', this.store.find('producttype'));
}
});
App.ProducttypesController = Ember.Object.create({
content: null
});
App.ProductsubtypesController = Ember.Object.create({
content: null
});
App.ProductsubtypeController = Ember.ArrayController.extend({
active: function() {
alert(""); }.property("App.selectedProductType.content").cacheable()
});
//model
App.Producttype = DS.Model.extend({
name: DS.attr(),
});
App.Productsubtype = DS.Model.extend({
name: DS.attr(),
});
var producttypes = {
"producttypes":[
{"id":1,"name":"type1"},
{"id":2,"name":"type2"},
{"id":3,"name":"type3"}
]
};
var productsubtypes = {
"productsubtypes":[
{"id":4,"name":"type4","type_id":"1"},
{"id":5,"name":"type5","type_id":"1"},
{"id":6,"name":"type6","type_id":"3"}
]
};
$.mockjax({
url: '/producttypes',
dataType: 'json',
type: 'get',
responseText: producttypes
});
$.mockjax({
url: '/productsubtypes',
dataType: 'json',
type: 'get',
responseText: productsubtypes
});
我找到了解决问题的方法。
控制器:
App.IndexController = Ember.Controller.extend({
selectedProductTypeChanged: function(){
var obj = this.get('selectedProductType');
if (obj != null) {
console.log(typeof obj.id);
var unfiltered=this.get('productsubtypes');
var filtered=this.get('productsubtypes').filterProperty("producttype",obj.id);
this.set('filterproductsubtypes',filtered );
this.set('selectedProductSubType',null);
}
}.observes('selectedProductType')
});
Link 到 jsbin:http://jsbin.com/juvokacuno/3/edit?html,js,output