我正在研究Ember.js,我遇到了一个问题我正在创建一个示例应用程序,我需要将客户端值发送到服务器端但我不知道该怎么做我知道传统方式如下面的代码
function create() {
var data = {
'EmailID': $('#emailid').val(),
'password': $('#password').val()
}
$.ajax({
url: '/EmberNew/Home/Create',
type: 'POST',
data:data,
success: function (response) {
alert("hi");
}
});
return false;
}
但是在Ember我不知道怎么做我现在的代码在下面给出
//Application
App = Em.Application.create();
//Model
App.Users = Em.Object.extend({
name: null,
password:null
});
//View
App.UserTextField = Em.TextField.extend({
insertNew: function () {
App.alertController.alertDetails();
}
});
App.PassTextField = Em.TextField.extend({
insertNew: function () {
App.alertController.alertDetails();
}
});
//controller
App.AlertController = Em.ObjectController.extend({
content: [],
username: '',
password: '',
alertDetails: function () {
var me = this;
var username = me.get("username");
var password = me.get("password");
alert('The User Name Is' + 'username' + 'And Password Is' + 'password');
}
});
App.alertController = App.AlertController.create();
我从alertDetails函数获取了文本框值,如何将它们传递到服务器端
答案 0 :(得分:0)
App.Record = Ember.Object.extend({
name: '',
other: ''
}).reopenClass({
records: [],
find: function() {
var self = this;
$.ajax({
url: "/api/records/",
type: "GET",
cache: false,
dataType: "json",
beforeSend: function() {
//if you want to call this often and need to clear + reload it
return self.records.clear();
},
success: function(results) {
var result;
for (_i = 0, _len = results.length; _i < _len; _i++) {
result = results[_i];
self.records.push(self.records.addObject(App.Record.create(result)));
}
},
error: function() {
return alert("error: failed to load the records");
}
});
return this.records;
}
});
现在您已经进行了模型设置,可以从路径模型钩子中调用它
App.RecordsRoute = Ember.Route.extend({
model: function() {
return App.Record.find();
}
});
find方法立即返回一个空数组,然后将模板绑定到它。当ajax调用返回w /成功并且您更新该数组时,把手模板将为您更新它而没有任何DOM或jQuery粘合代码