Das hinzufügen neuer Modelle, um eine backbone-Sammlung, nicht ersetzen

Ich versuche, neue Modelle zu einer Kollektion (ich bin mir nicht gespeichert auf dem server dieser Zeit, genau dies zu tun, in-memory). Ich habe den folgenden code:

$(function () {

//Model

var Story = Backbone.Model.extend({

  defaults: {
    'title': 'This is the title',
    'description': 'Description',
    'points': 0
  },

  url: '/'

});

//Collection

var Stories = Backbone.Collection.extend({

  model: Story,

  url: '/'

});

//View

var BaseView = Backbone.View.extend({

  el: $('#container'),

  events: {
    'click .inner': 'onClickInner'
  },

  onClickInner: function() {

    this.options.x++;

    this.story.set({
      'title': 'This is my collection test ' + this.options.x,
      'description' : 'this is the description'

    });

    this.stories.add(this.story);

    this.render();

  },

  initialize: function () {

    this.stories = new Stories();
    this.story = new Story();

  },

  render: function(){

      console.log(this.stories);

  }

});

//Initialize App

  var app = new BaseView({
    'x' : 0
  });

});

Meine Frage ist, für jede Zeit 'onClickInner' läuft, möchte ich hinzufügen, dass ein neues Modell der Kollektion. Jedoch, in meinem code ist es ersetzt das bestehende Modell in die Sammlung. Wie füge ich neue Modelle und nicht ersetzen?

Vielen Dank für Ihre Zeit.

InformationsquelleAutor CarbonDry | 2013-08-18

Schreibe einen Kommentar