我正在使用 ember js 构建一个应用程序,但我不确定如何将 jQuery 与 ember 一起使用。我想要实现的一个示例是从我的 api 获取销售数据的页面。
在我的 app.js 中我有
App.TransactionsRoute = Ember.Route.extend({
model: function() {
return App.Transaction.find();
}
});
如果我将开始日期和结束日期传递给 App.Transaction.find() 方法,我的 api 会将其发送回的数据限制在这些日期内。
我在页面上有一个表单,其中包含开始日期和结束日期的输入,我想将其连接到 jQuery UI 的日期选择器。
这就是我被困的地方我把它放在我的 ember 应用程序代码的末尾
jQuery(document).ready(function() {
jQuery("#transactionsStartDate").datepicker();
jQuery("#transactionsEndDate").datepicker();
});
但它不做任何事情,也不会抛出任何错误。
我怎样才能让jquery运行?另外,如何将输入的日期变量连接到调用App.Transaction.find({startDate: startDateVariable, endDate: endDateVariable})
感谢您的帮助!
EDIT更正 jQuery 正在运行,但它在渲染视图之前运行。 ember 是否有一个钩子或可以在渲染视图时调用的东西?
而不是将 jQuery(document).ready() 中的 datepicker 初始化挂钩放入视图的 didInsertElement 方法中
App.TransactionsView = Ember.View.extend({
templateName: 'transactions',
didInsertElement: function() {
jQuery("#transactionsStartDate, #transactionsEndDate").datepicker();
}
});
使用 Ember 内置的 TextField 视图
<script type="text/x-handlebars" data-template-name="transactions">
<form>
<label for="transactionsStartDate">Start Date</label>
{{view Ember.TextField id="transactionsStartDate" valueBinding="startDate"}}
<label for="transactionsEndDate">End Date</label>
{{view Ember.TextField id="transactionsEndDate" valueBinding="endDate"}}
<button {{action fetchTransactions}}>Fetch Data</button>
</form>
</script>
并在控制器上定义操作 fetchTransactions
App.TransactionsController = Ember.ArrayController.extend({
fetchTransactions:function() {
this.set('model', App.Transaction.find({startDate:this.get('startDate'), endDate:this.get('endDate')}));
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)