← Back to Upcase

Managing simple spreadsheet alike interface with in Backbone.JS


(Yevgeniy Viktorov) #1

Hello,

looking for the options to implement the subj with requirements specified below.

Let’s say we’ve a table where some fields can be edited, the each record represent the model of certain collection, and there is only one “Update” button below, when clicked it should push updated records to the server, i.e. model.save or collection.invoke(‘save’). Additionally I need a way to inform the user with a single message when all records are persisted or some are failed.

Currently the view portion is implemented within marionette’s composite and item views, “Update” button, located in composite view, triggers an event, then in controller we’ve a loop for child views:

listSubmit: ->
  promises = []

  @listView.children.each (view) =>
    data = Backbone.Syphon.serialize view
    promises.push @processListSubmit(data, view.model)

  @checkStatus promises

processListSubmit: (data, model) ->
  promise = $.Deferred()

  # set events
  @listenTo model, 'sync', promise.resolve
  @listenTo model, 'error', promise.reject

  # persisting new data
  model.save data

  # return promise
  promise

checkStatus: (promises) ->
  $.when(promises...)
    .done ->
      # TODO: trigger app event instead
      alert('Variants were saved successfully')
    .fail ->
      # TODO: trigger app event instead
      alert('There was an error saving variants')

Thanks in advance.