javascript - Backbone model save() method not doing anything -
i'm using backbone rails , trying save new model server. however, calling .save()
on model doesn't seem doing anything. looking @ "network" tab in chrome, don't see request being made. here's code:
model
class myapp.models.chain extends backbone.model defaults: steps: [] id: null urlroot: '/chains' validate: (attrs) -> if $.trim(attrs.steps[@step_number - 1]) == '' return "step can't blank."
view
class myapp.views.chainsnew extends backbone.view # @template defined conditional inside render() initialize: -> @is_saving = false events: 'click #btn-go': 'add_step' 'click #btn-done': 'save' render: (step_number) -> @model.step_number = step_number @template = if @model.step_number 1 jst['chains/new'] else jst['chains/step'] $(@el).html(@template({previous_step_text: @model.get('steps')[step_number - 2]})) @ add_step: -> #divide array arrays of steps before , after step being edited steps = @model.get('steps') array1 = steps.slice(0, @step_number - 1) array2 = steps.slice(@step_number) array1.push(@$el.find('textarea').val()) newarray = array2.concat(array1) if !@model.set({steps: newarray}, {validate: true}) alert(@model.validationerror) false else true #need return value `save` method save: -> @is_saving = true if @add_step @model.save(null, { success: -> console.log(arguments) error: -> console.log(arguments) })
i know @model.save
being called because have logged message right before console (in other words if @add_step
true).
most likely, model failing validation, makes save
return false , make no api call. try with
class myapp.models.chain extends backbone.model defaults: steps: [] id: null urlroot: '/chains' validate: (attrs) -> if $.trim(attrs.steps[@step_number - 1]) == '' console.log "houston, have problem" return "step can't blank."
if see message in console, there's issue. can try comment validate
function, , api call should made in circumstances.
Comments
Post a Comment