Feature: Delete a project

Let's add a delete button to our form for the feature Delete a project.

Let's make a change to ProjectView and add a new event to the events hash, called delete, which calls the delete method. We add a delete method, which destroys the model and removes ProjectView. We then call repository, removing RepositoryListView.

    events: {
      ...
      "click button.delete": "delete",
    },

    delete: function () {
      this.model.destroy();
      this.remove();
      this.repository({editMode:false});
    },

Let's make a change to ProjectListView and add a collection event handler to initialize. The event handler calls the remove method when an item is removed. The remove method grabs the model's attributes and searches the Projects collection, removing the item when finding it.

    initialize: function () {
      ...
      this.collection.on("remove", this.remove, this);
    },

    remove: function (removedModel) {
      var removed = removedModel.attributes;

      _.each(this.Projects, function (project) {
        if (_.isEqual(project, removed)) {
          this.Projects.splice(_.indexOf(projects, project), 1);
        }
      });
    },

You will now be able to delete a project by clicking on the delete button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.233.22