Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do this to save your model:</p> <pre><code>@model.save null, success: -&gt; ... error: -&gt; ... </code></pre> <p>That <code>null</code> is the source of your trouble, use <code>{}</code> and things will start behaving better; if you combine your <code>@model.set</code> and <code>@model.save</code> calls, things will be even better:</p> <pre><code>attrs = subject: $("#js-subject").val() #... @model.save attrs, success: -&gt; ... error: -&gt; ... </code></pre> <p>A <a href="http://documentcloud.github.com/backbone/#Model-save"><code>save</code></a> call looks like this:</p> <blockquote> <p><strong>save</strong> <code>model.save([attributes], [options])</code><br> [...]<br> The <strong>attributes</strong> hash (as in <code>set</code>) should contain the attributes you'd like to change</p> </blockquote> <p>So passing a <code>null</code> for <code>attributes</code> means that you want to save the model as it is.</p> <p>When you <a href="http://documentcloud.github.com/backbone/#Model-save"><code>save</code></a> a model, the validation is mostly left up to <a href="http://documentcloud.github.com/backbone/#Model-set"><code>set</code></a>, the <a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L360">code looks like this</a>:</p> <pre><code>if (attrs &amp;&amp; !this.set(attrs, options.wait ? silentOptions : options)) { return false; } </code></pre> <p>Your <code>attrs</code> will be <code>null</code> so <code>set</code> will not be called; however, if you let <code>save</code> handle your <code>set</code>, you will get the behavior you're after. If you passed the <code>wait: true</code> option, <code>save</code> would <a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L353">manually run the validation</a> on the passed attributes:</p> <pre><code>if (options.wait) { if (!this._validate(attrs, options)) return false; ... } </code></pre> <p>The internal <a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L524"><code>_validate</code></a> method is a wrapper for <code>validate</code> that does some bookkeeping and error handling. You're not using <code>wait: true</code> so this doesn't apply to you but I thought it was worth mentioning anyway.</p> <p>Consider a simple example with a model whose <code>validate</code> always fails. If you say this:</p> <pre><code>@model.on 'error', @error @model.save attrs, success: -&gt; console.log 'AJAX success' error: -&gt; console.log 'AJAX error' </code></pre> <p>then <code>@error</code> will be called because <code>save</code> will end up calling <code>set</code> with <a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L343">some attributes</a> and <code>set</code> will call <code>validate</code>. Demo: <a href="http://jsfiddle.net/ambiguous/HHQ2N/1/">http://jsfiddle.net/ambiguous/HHQ2N/1/</a></p> <p>But, if you say:</p> <pre><code>@model.save null, ... </code></pre> <p>the <code>null</code> <a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L360">will cause the <code>set</code> call to be skipped</a>. Demo: <a href="http://jsfiddle.net/ambiguous/6pX2e/">http://jsfiddle.net/ambiguous/6pX2e/</a> (the AJAX here will fail).</p> <p>Your <code>@model.set</code> call right before <code>@model.save</code> should be triggering your error handler but if you don't check what <code>@model.set</code> returns, execution will blindly continue on to the <code>save</code> call and talk to your server.</p> <hr> <p>In summary, you have three things going on here:</p> <ol> <li>You're not calling <code>save</code> they way you should be.</li> <li>You're ignoring the <code>@model.set</code> return value and losing your chance to trap the validation errors.</li> <li>Backbone's argument handling for <code>save(null, ...)</code> could be better but I don't know if it is worth the effort to handle a strange way of calling it.</li> </ol> <p>You should combine your <code>set</code>/<code>save</code> pair into just a <code>save</code> or check what <code>set</code> returns.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload