
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
backbone-forkable-model
Advanced tools
Extend Backbone.js models to make them forkable from another model, and later able to be merged back to the original.
The original use case for this was that I had a lot of UI views that let users edit models in a modal window (popup editor form).
The modal dialog would always have "Cancel" and "Save" buttons.
On close, I would need to revert the model back to its original state. We got in the habbit of passing a .clone() of the model into the editor window as the Model instead of the "real" copy of the model that was loaded from the server. Then when "cancel" was clicked, no action had to be taken. The cloned copy of the model that the editor was using could just be discarded. On "save", the cloned (edited) model's attributes could be copied back tot he "main" copy with a simple original.set(cloned.attributes);
In spirit, that is what this model mixin provides.
(not yet published to NPM and Bower. It is on my todo list!)
Add to page scripts after backbone.js.
This library does not support Require.js or Browserify at this time (in other words, it is not written as a module).
This mixin attaches itself to the global Backbone object as Backbone.ForkableModel.
Extend a Backbone model (or backbone-deep-model):
var MyModel = Backbone.Model.extend(Backbone.ForkableModel.mixin).extend({
yourCustomStuff: function() {},
goesInHere: function() {}
});
You can chain .extend() calls to add more mixins. For example I typically use DeepModel and the Backbone.Validation library:
var MyModel = Backbone.DeepModel
.extend(Backbone.ForkableModel.mixin)
.extend(Backbone.Validation.mixin)
.extend({
yourCustomStuff: function() {},
goesInHere: function() {}
});
The ForkableModel.mixin adds these functions to instances of the model:
Returns a new instance of the model wil all attributes copied tot he new instance. This is bascially the same as Model.clone();
Sets the attributes of the instance of the model that was originally forked to match this forked copy (Copies property values back to the original).
This is done using Model.set() and Model.unset() functions internally, so all events will be triggered on the model instance as if .set() was called normally.
Returns the instance of the model that was orignally forked from.
Returns boolean, true if the forked copy of the model has any changes from the original. Otherwise returns false.
Marionette.ItemView.extend({
events: {
'click #editButton': 'onEdit'
},
// When the user wants to edit this item, an edit form is opened.
onEdit: function() {
var editDialog = new EditDialogView({
model: this.model.fork()
});
// the EditDialogView triggers a "save" event when the user clicks the save button
this.listenTo(editDialog, 'save', function(editedModel) {
var originalModel = editedModel.unfork();
// originalModel is the same instance as "this.model" that .fork() was called on.
originalModel.save().done(function() {
// notify user of successful save
}).fail(function() {
// notify user of error
});
});
editDialog.show();
}
});
FAQs
Extend Backbone.js models to be forked and merged.
The npm package backbone-forkable-model receives a total of 20 weekly downloads. As such, backbone-forkable-model popularity was classified as not popular.
We found that backbone-forkable-model demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.