好吧,我在这里有一个backbone.js 模型
define ['jquery','underscore','backbone'] , ($,_,Backbone) ->
class Student extends Backbone.Model
idAttribute : "UserKey"
urlRoot : "Api/Student"
defaults:
UserKey: null
FirstName : ""
LastName : ""
Username : ""
Password : ""
Age : 18
initialize : ->
#console.log "You have been initialized"
让我们看看我这里有一个控制器:-
public class HomeController : Controller
{
private Repository<Student, PortalContext> repo;
public HomeController()
{
repo = new Repository<Student, PortalContext>(new PortalContext());
}
[HttpGet]
public ActionResult All()
{
IList<Student> studs = repo.GetAll().ToList<Student>();
return this.Json(new { Students=studs }, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult Get(int id)
{
Student student = repo.FindBy(x => x.UserKey ==id).Select(x=>x).SingleOrDefault<Student>();
return this.Json(student,JsonRequestBehavior.AllowGet);
}
[HttpPut, ActionName("Student")]
public ActionResult Update(Student student)
{
repo.Edit(student);
repo.Save();
return Json(student);
}
}
这是我保存模型的 View.Coffee 文件:
define ['jquery'
,'underscore'
,'backbone'
,'text!/Templates/Student/View.htm'] , ($ , _ , Backbone , ViewTemplate) ->
class StdView extends Backbone.View
_.templateSettings = { interpolate: /\{\{(.+?)\}\}/g };
template : _.template(ViewTemplate)
events : ->
"click #back":"Back"
"click #save" : "Save"
Save : ->
console.log "tryng to save"
@model.save
'FirstName' : @$el.find("#txtFirstName").val()
'LastName' : @$el.find("#txtLastName").val()
'Age': @$el.find("#txtAge").val()
,
wait:true
success: =>
alert "Saved"
@
Back: (e)->
window.history.back()
false
initialize : (options) ->
@render()
render :->
#console.log "this is details"
@setElement @template @model.toJSON()
@
它有时会保存模型,但有时会回传到服务器,即骨干网不保存数据,而是像这样将数据附加到浏览器 url
并回帖。但有时它会保存数据并显示警报。我不确定为什么会发生这种情况有人可以说明为什么会这样吗??