我有一个国家/地区下拉列表,根据所选国家/地区,我想填写一个城市下拉列表.
有人可以根据另一个下拉选项提供一个如何填充下拉列表的示例吗?
我已经基于这篇文章实现了身份验证
因此我App.Session在应用程序初始化时创建一个对象
Ember.Application.initializer({
name: 'session',
initialize: function (container, application) {
App.Session = Ember.Object.extend({
init: function () {
this._super();
this.set('authToken', $.cookie('auth_token'));
this.set('authAccountId', $.cookie('auth_accountId'));
this.set('authAccountLanguage', $.cookie('auth_accountLanguage'));
},
authAccountIdChanged: function () {
var authAccountId = this.get('authAccountId');
$.cookie('auth_accountId', authAccountId);
//Load the actual account record from the server if the authAccountId is set
//Used to have for example the full name or other properties of the account
if (!Ember.isEmpty(authAccountId)) {
this.set('authAccount', this.store.find('account', authAccountId));
}
}.observes('authAccountId'),
...
Run Code Online (Sandbox Code Playgroud)
我有观察员authAccountId; 因此,每次accountId …
我正在以预算经理的形式使用Ember编写一个小测试应用程序.我有一个Budget对象,它包含一般属性,如每月限制和描述.我还有一组Expense对象,其中包含名称,花费的金额等.两者都是使用Ember Data的REST适配器从服务器检索的.
HTML:
<body>
<script type="text/x-handlebars" data-template-name="budget">
<h2>{{name}} (€ {{amount}})</h2>
</script>
<script type="text/x-handlebars" data-template-name="expenses">
<ul id="expense-list">
{{#each model}}
{{render "expense" this}}
{{/each}}
</ul>
</script>
<!-- expense template -->
<script type="text/x-handlebars" id="expense">
<li>
<label>{{description}}</label>
<label class="subtle">{{formatDate time}}</label>
<label class="amount">{{amount}}</label>
</li>
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JavaScript:
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:5000',
namespace: 'outthepocket/api'
});
// Model
App.Expense = DS.Model.extend({
amount: DS.attr('number'),
description: DS.attr('string'),
time: DS.attr('date')
});
App.Budget = DS.Model.extend({
name: DS.attr('string'),
amount: DS.attr('number')
});
// Routes
App.Router.map( function() { …Run Code Online (Sandbox Code Playgroud) 我们怎样才能观察到Ember中数组的变化?我有使用模型中的原始对象数据的ember组件,但为了演示我在组件本身中使用数组属性的问题,如:
init:function(){
this._super();
var arr = Ember.A([
Ember.Object.create({"shelfNo": 1, "noOfItems": 2}),
Ember.Object.create({"shelfNo": 2, "noOfItems": 3}),
Ember.Object.create({"shelfNo": 3, "noOfItems": 2})]
);
this.set("someArray",arr);
//DOES NOT WORK
this.get('someArray').addArrayObserver(function () {
Ember.ObjectController.create({
arrayWillChange: function (observedObj, start, removeCount, addCount) {
console.log("in arrayWillChange");
},
arrayDidChange: function (array, start, removeCount, addCount) {
console.log("in arrayDidChange");
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
testObserver: function(){
//DOES NOT WORK
console.log("here");
}.observes('someArray@each')
Run Code Online (Sandbox Code Playgroud)
但两者都不适合我!
这是一个jsbin:http://jsbin.com/EkumOjA/1/
谢谢,迪
试图理解为什么它不起作用.
我有类似的东西.
<div class="carousel slide" id="new-prospect-container">
<div class="carousel-inner">
{{#each model}}
<div class="item">
...
</div>
{{/each}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是Botostrap的第一个类api意味着我们不需要执行任何JS方法,它们的小部件将自动运行.问题是我怀疑Bootstrap会在我的{{model}}被Ajax请求填满之前执行此操作.所以这个Carousel将不起作用.
令人讨厌的是我已经尝试关闭他们的数据-api - $(document).off('.data-api'); 并手动调用他们的轮播方法 - 仍然无法正常工作.
旋转木马使用硬编码数据 - 但是一旦我尝试从我的Ember模型填充旋转木马div项目,它就会停止工作.
在最新的ember-data 1.0发布之后,我在创建记录时遇到了一些问题.我在发行说明中读到了 - https://github.com/emberjs/data/blob/master/TRANSITION.md:
App.NewPostRoute = Ember.Route.extend({
model: function() {
return App.Post.createRecord();
}
});
Run Code Online (Sandbox Code Playgroud)
现在替换为:
App.NewPostRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('post');
}
});
Run Code Online (Sandbox Code Playgroud)
但是在我的控制器中我无法弄清楚如何调用createRecord()方法,因为我有这样的东西:
addNewTrip:function() {
var tripDeparature = this.get("tripDeparature");
var tripArrival = this.get("tripArrival");
var trips = App.Trips.createRecord({
tripDeparature: tripDeparature,
tripArrival:tripArrival,
isCompleted:false
});
trip.save();
this.set("tripDeparture","");
this.set("tripArrival","");
}
Run Code Online (Sandbox Code Playgroud)
并且它抛出一个错误:...没有方法'createRecord'(在新版本之后是预期的),但我无法弄清楚如何正确调用createRecord.任何帮助是极大的赞赏.
我希望能够将我的Session单身注入我的Ember模型中.我试图支持的用例是在模型上计算了对用户配置文件作出反应的属性(Session对象上的属性).
App = window.App = Ember.Application.create({
ready: function() {
console.log('App ready');
this.register('session:current', App.Session, {singleton: true});
this.inject('session:current','store','store:main');
this.inject('controller','session','session:current');
this.inject('model','session','session:current');
}
});
Run Code Online (Sandbox Code Playgroud)
注射可以很好地控制到控制器,但我遇到麻烦model.这里有限制吗?任何特殊技术?
--------附加背景---------
这是我希望能够在我的model定义中做的一个例子:
App.Product = DS.Model.extend({
name: DS.attr("string"),
company: DS.attr("string"),
categories: DS.attr("raw"),
description: DS.attr("string"),
isConfigured: function() {
return this.session.currentUser.configuredProducts.contains(this.get('id'));
}.property('id')
});
Run Code Online (Sandbox Code Playgroud)