0

My app is Backbone.js for client-side, Express.js for back-end.

I have problems with syncing with all parts of my API, using the backbone model and collection(they use urlRoot: "/users"). I'm allowed to use only GET or POST, no PUT or DELETE. *I'm not allowed to use more models* Not allowed to use jQuery ajax

My API

add new user: I need to make a POST to /users with JSON of new data. So I did it just fine with - this.model.save({new data...})

list all users: My API for that, responses to GET /users, with the right handler - so, this.collection.fetch() - works fine.

Log-In: My API accepts POST to /users/login for that. How can I add a function "logIn" to my model, that will use custom sync/pass options.url to sync - or any other way - that will POST to /users/login ?

Log-Out: API accepts POST to /users/logout - how to send this request using my backbone model ?

User By ID: Same question here for GET /users/:id

Update user: POST /users/:id - same question again.

--- So actually, the question in short is ---

What is the best way (or the most "right"), to implement methods of a backbone model, that are similar to "model.save()" - but just POST/GET to a bit different path then urlRoot ?

Daniel
  • 1,562
  • 3
  • 22
  • 34

1 Answers1

1

You probably have a couple options here. One would be structuring your models in a way that supports the urls you want. For instance, have a User model and a Session model that deal with updating the user and managing the logged in state separately.

The other thing you should probably do is to use the url method in your models.

Something like this in your User model. (Note: using urlRoot instead of url here is identical, but this is the correct approach for anything more complicated that is needed in the url)

url : function() {
  var base = "/users/";
  if(this.isNew()) {
    return base;
  } else {
    return base + this.get("id");
  }
}

You could extend this same concept to work in your Session model for handling logout vs login based on if the Session is new or not.

Update:

If you must use the same model, the best thing would be to totally bypass the Backbone.sync method and write a custom AJAX call with success/error handlers that know how to clean things up.

login : function() {
  $.post("/users/login", {success: function (response) {
      // Update user as needed
    }, error: function (xhr, response) {
      // Handle errors
    }
  }
}
Andrew Hubbs
  • 9,338
  • 9
  • 48
  • 71
  • Thanks. I'm not allowed to use more models, all logic should be inside the user model/collection. About the second option - but then how can I POST to /users/login ... ? – Daniel Nov 19 '12 at 00:05
  • I don't understand why you are not allowed to use more models. If you want to work within the structure of Backbone, then that is the correct way to do it. Fundamentally, you should view the Session as an object that is separate from the User. – Andrew Hubbs Nov 19 '12 at 00:07
  • If you really want to do this then you should handle the AJAX completely independently of the Backbone model. – Andrew Hubbs Nov 19 '12 at 00:08
  • reason why - my boss. He also doesn't allow me to use jQuery ajax. He wants me to make custom SYNC for the user model, and use methods that use this SYNC. – Daniel Nov 19 '12 at 00:10
  • Ok. Yes, you could rewrite `Backbone.sync` if you want (although that doesn't make much sense because you are trying to override most of what it does to just use the pure AJAX underneath). The right way to use `Backbone.sync` here is to do what I originally said and create a separate `Session` model. – Andrew Hubbs Nov 19 '12 at 00:12
  • Thanks Andrew, I can understand your logic - but i'm not allowed... :/ – Daniel Nov 19 '12 at 00:14
  • I'm not going to implement a custom `sync` method here, but I can tell you that if you want to do so, `Backbone` is setup to easily use such a method by simply defining it on the `User` model as needed. There are good example of how to do so on SO already. Such as this one: http://stackoverflow.com/questions/5096549/how-to-override-backbone-sync – Andrew Hubbs Nov 19 '12 at 00:17