-
Notifications
You must be signed in to change notification settings - Fork 120
sync
Lance Pollard edited this page Sep 24, 2012
·
2 revisions
Global method that you override
Pros:
- Easy to get up and running with
- One entry point
Cons:
What if I don't want to sync, just mess around with models on the client? I can create another store... What if I want to sync in batches? Complex store extending store then...
Extend the basic store.
There are several ways you can define a url:
class App.Post extends Tower.Model
@sync "/articles"
class App.Comment extends Tower.Model
@sync belongsTo: "Post"
class App.Post extends Tower.Model
sync: "/articles"
class App.Comment extends Tower.Model
sync: (method) ->
"/articles/#{@post().get("id")}/comments"
class App.PostsController extends App.ApplicationController
@on "syncCreateSuccess"
syncCreateSuccess:
record.save sync: false, (error) =>
unless error
create: ->
@_create (success, failure) =>
success.html ->
@render "admin/posts/index"
@resource.sync "create", (error) =>
success.ajax ->
create: ->
resource.save (error) =>
resource.sync
afterCreate: ->
afterSyncCreate: ->
Post.sync().create()
Post.createSync()
Post.create()
Post.sync()
post.updateAttribute
post.save sync: false, (error) =>
post.destroy =>
post.sync()
post.save(sync: true)
Tower.Model.Sync =
sync: ->
syncAction = @syncAction
@runCallbacks "sync", =>
@runCallbacks "#{syncAction}Sync", =>
@store["#{syncAction}Sync"](@)
updateSyncAction: (action) ->
@syncAction = action # create, update, delete
# if it was create, and it's never been synced, then we can just remove it from memory and be all cool
when "delete" then "delete"
switch @syncAction
when "update" then "delete"
else
action
else
action
when "update"
switch @syncAction
when "create" then "create"
else
"update"
Tower.Store =
readSync: ->
@sync.read
createSync: ->
@sync.create
updateSync: ->
@sync.update
destroySync: ->
@sync.destroy