backbone.js change url parameter of a Model and fetch does not update data fetched

后端 未结 4 505
再見小時候
再見小時候 2021-02-04 10:28

I have the following Model:

window.MyModel = Backbone.Model.extend({
     initialize: function(props){
          this.url = props.url;
     } 

     parse: funct         


        
4条回答
  •  醉话见心
    2021-02-04 10:28

    Well the answer here is that you want to do:

    mod.url = '/some/other/url'
    

    The URL isn't part of the instance of the model itself, but rather an attribute of the MyModel object that you're creating your model instance from. Therefore, you'd just set it like it was an normal JavaScript object property. set is used only when the data you're setting (or conversely getting with get) is actually an attribute of the data you want to send/receive from the server.

    But why you're changing the URL is the question we should be asking. The idea behind Backbone's model/collection system is that you speak to a REST endpoint and each model has a corresponding endpoint.

    Like you've got a blog and that blog has an "entry" object which is available at:

    /rest/entry/
    

    And you've got a Backbone model for Entry:

    Entry = Backbone.Model.extend({urlBase: '/rest/entry'});
    

    Now when you save or fetch Backbone knows how this works.

    So like you're making a new model:

    e = new Entry();
    e.set({title: "my blog rulez", body: "this is the best blog evar!!!!1!!"});
    e.save();
    

    This would then make Backbone do an HTTP POST request to /rest/entry with the body:

    {
      "title": "my blog rulez",
      "body": "this is the best blog evar!!!!1!!"
    }
    

    (When you do your mod.set({url: '/some/other/url'}); you're actually adding a field called url to the dataset, so the server would send "url": "/some/other/url" as part of that JSON POST body above:

    {
      "title": "my blog rulez",
      "body": "this is the best blog evar!!!!1!!",
      "url": "/some/other/url" 
    }
    

    The server would then respond with an HTTP 200 (or 201) response with the same model, only with, like, say, and ID attached:

    {
      "id": 1,
      "title": "my blog rulez",
      "body": "this is the best blog evar!!!!1!!"
    }
    

    And that's not what you're looking for, right?)

    Now you've got this model and it's got an ID. This means if you change it:

    e.set('title', 'my blog is actually just ok');
    e.save()
    

    Backbone now makes an HTTP PUT request on /rest/entry/1 to update the resource on the server.

    The server sees that you're talking about ID 1 on the /rest/entry/ endpoint, so knows to update an existing record (and send back an HTTP 200).

    TL;DR

    Don't change the URL, Backbone will. Make a new model for a new piece of data.

提交回复
热议问题