Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbonejs collection.create: how to prevent it from adding items if there is an error?
    primarykey
    data
    text
    <p>How can I prevent <code>collection.create</code> from adding items into its collection if there is an error found in the input?</p> <p>html,</p> <pre><code>&lt;div id="new-status"&gt; &lt;h2&gt;New monolog&lt;/h2&gt; &lt;form action=""&gt; &lt;textarea name="content"&gt;&lt;/textarea&gt;&lt;br&gt; &lt;input type="submit" value="Post"&gt; &lt;/form&gt; &lt;/div&gt; &lt;div id="statuses"&gt; &lt;h2&gt;Monologs&lt;/h2&gt; &lt;ul&gt;&lt;/ul&gt; &lt;/div&gt; </code></pre> <p>backbone,</p> <pre><code>var Status = Backbone.Model.extend({ initialize: function(){ }, validate: function(attrs, options) { if(attrs.text === '') alert("please enter some text"); }, url:"dummy.php", sync: function (method, model, options) { return $.ajax({ type: "POST", dataType: 'json', url: 'server.php', data: { text: this.get("text") } }); } }); var Statuses = Backbone.Collection.extend({ model: Status }); var NewStatusView = Backbone.View.extend({ events: { "submit form": "addStatus" }, initialize: function(options) { _.bindAll(this, 'addStatus', 'clearInput'); this.listenTo(this.collection, 'add', this.clearInput) ; }, addStatus: function(e) { e.preventDefault(); this.collection.create({ text: this.$('textarea').val() }); }, clearInput: function() { this.$('textarea').val(''); } }); var StatusesView = Backbone.View.extend({ initialize: function(options) { this.collection.on("add", this.appendStatus, this); }, appendStatus: function(status) { this.$('ul').append('&lt;li&gt;' + status.escape("text") + '&lt;/li&gt;'); } }); $(document).ready(function() { var statuses = new Statuses(); new NewStatusView({ el: $('#new-status'), collection: statuses }); new StatusesView({ el: $('#statuses'), collection: statuses }); }); </code></pre> <p>So, when you hit the submit button without typing any text, you get an error popup from this part in the model,</p> <pre><code>validate: function(attrs, options) { if(attrs.text === '') alert("please enter some text"); }, </code></pre> <p>But how can I <strong>tell the collection</strong> that there is an error and <strong>do not add</strong> this empty item and also <strong>do not fire</strong> <code>sync</code> in the model?</p> <p><strong>EDIT:</strong></p> <p>got it worked with <code>collection.create</code> in this way...</p> <p>model,</p> <pre><code>validate: function(attrs, options) { if(attrs.text === '') { var message = "please enter some text"; alert(message); return message; } }, </code></pre> <p>view,</p> <pre><code>addStatus: function(e) { e.preventDefault(); var one = new Status({ text: this.$('textarea').val() }); if (!one.isValid()) { alert(one.validationError); } else { this.collection.create(one); } }, </code></pre> <p>it seems to work fine unless it is not a good approach or against MVC pattern?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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