表单是在为backbone.js restful url put方法调用save时发布的



我这里有一个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中,如

http://abc.com/?firstname=Joy&姓氏=罗伊&年龄=19#浏览/1

并张贴回。但有时它会保存数据并显示警报。我不知道为什么会发生这种事有人能解释一下为什么会这样吗?

经过我们的讨论,Nettuts刚刚提供了一个20分钟的视频教程,我相信它会比我更好地帮助你。看一看,如果你还有任何问题,请告诉我。他正在使用Laravel作为他的服务器,但据我所知,你的问题不在于你的服务器,而在于你的客户端应用程序。这是视频。

最新更新