backbone-ws icon indicating copy to clipboard operation
backbone-ws copied to clipboard

create tutorial

Open goti2 opened this issue 8 years ago • 13 comments

It would be cool to do a test project(backbone.ws)

goti2 avatar Apr 12 '16 15:04 goti2

In the pipe... :construction_worker: Thanks @goti2!

ydaniv avatar Apr 13 '16 06:04 ydaniv

Is there any news?

smalot avatar Dec 12 '16 17:12 smalot

Hey @smalot, thanks for the kind ping (: Unfortunately I can't seem to be able to get to my projects, but if there's an interest I'll do my best.

ydaniv avatar Dec 13 '16 07:12 ydaniv

Many thanks, because it's very clear howto to connect multiple mode object and/or a collection

smalot avatar Dec 13 '16 13:12 smalot

Does this plugin support bi-directional communication ? Backbone => WS WS => Backbone

smalot avatar Dec 13 '16 13:12 smalot

@smalot let me see if I understand your question correctly:

With BBWS (Backbone.WS) you can handle server-client bi-directional communication:

  1. server => client: using ws:message events - and of course with custom routing you can achieve more complex scenarios).
  2. client => server: either by using .send() method on the Model/Collection, or by setting sync: true and simply calling .save(). The later will send data sent to .save() or default to the entire attributes of the model.

ydaniv avatar Dec 13 '16 13:12 ydaniv

Indeed, Gorgeous API ! But I confirm, a tuto is a must

smalot avatar Dec 13 '16 13:12 smalot

@smalot thanks! will do my best...

ydaniv avatar Dec 13 '16 13:12 ydaniv

One quick remark. When I create a new model object on client side and then call the .save() function, the following request is transferred:

{
  "id":null,
  "board_id":0,
  "title":"title",
  "created":0,
  "updated":0,
  "method":"create" //
}

I note that the method attribute has been added by the API. But when method is defined nothing is sent. Prefixing it by an underscore should be a good idea, or a better solution would be to create a 2 levels structure with the data in a key, and the method in another one.

{
  "data": {
    "id":null,
    "board_id":0,
    "title":"title",
    "created":0,
    "updated":0
  },
  "method": "update"
}

smalot avatar Dec 13 '16 14:12 smalot

@smalot correct, right here.

What do you mean by "nothing is sent"? From what I see it should send everything just the same, only using your predefined method instead of adding Backbone's method.

What kind of behavior exactly do you see?

ydaniv avatar Dec 13 '16 14:12 ydaniv

Honestly, I don't remember what I wanted to say by "But when method is defined nothing is sent.".

My main remark was to separate the real data extracted from the model (via toJSON()) against the method (create/update/...). The second solution seems to be the best and is quite similar to the message exchanged from server to the client ({data: [], type: ''}). I'm still working on your API to understand how everything is working.

smalot avatar Dec 13 '16 22:12 smalot

@ydaniv can I use you lib to sync a collection ? I'm encountering xhr issues

-- edited --

my goal is to push to server a collection of model for a global insert or update in order to avoid multiple calls

smalot avatar Dec 20 '16 10:12 smalot

@smalot if you call collection.send() this should work as expected. If what you want is to call collection.save() together with useSync: true and have it persist to the server, this should also work as expected, except for the method property, which now I see is not handled correctly. It's only handled for Models, and I should change it to add method prop to each model sent. Currently it will send the entire collection, unless you specify the data sent explicitly.

What exactly is your use case?

ydaniv avatar Dec 20 '16 11:12 ydaniv