{"id":18440270,"url":"https://github.com/drowzy/backbone.dirty","last_synced_at":"2025-04-14T15:26:52.071Z","repository":{"id":28763307,"uuid":"32285533","full_name":"drowzy/backbone.dirty","owner":"drowzy","description":"dirty-checking for Backbone models","archived":false,"fork":false,"pushed_at":"2018-08-14T23:44:03.000Z","size":144,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-16T11:14:10.060Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drowzy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-15T21:40:22.000Z","updated_at":"2015-03-21T20:19:28.000Z","dependencies_parsed_at":"2022-09-05T00:21:27.595Z","dependency_job_id":null,"html_url":"https://github.com/drowzy/backbone.dirty","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drowzy%2Fbackbone.dirty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drowzy%2Fbackbone.dirty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drowzy%2Fbackbone.dirty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drowzy%2Fbackbone.dirty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drowzy","download_url":"https://codeload.github.com/drowzy/backbone.dirty/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248905548,"owners_count":21181010,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T06:29:15.614Z","updated_at":"2025-04-14T15:26:51.898Z","avatar_url":"https://github.com/drowzy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# backbone.dirty\n\nSimple model dirty-checking for Backbone.js aka tracking of 'unsaved' state.\n\n## Installation\n\n### Node.js\n\n    npm install backbone.dirty\n\n### Bower builds\n\n    bower install backbone.dirty\n\n## Getting started\n\nThe plugin is tested with, and should work with the following versions of\n\n* Backbone \u003e= 1.1.2\n* Underscore \u003e= 1.8.3\n\n### Basic\nInclude backbone.dirty after Backbone and underscore\n\n\n```javascript\n\u003cscript src=\"path/to/backbone/dist/backbone.dirty.min.js\"\u003e\u003c/script\u003e\n```\n\n### node.js/browserify\nIf you use Backbone with node.js or browserify backbone.dirty can be\nrequired using \n```javascript\nvar Dirty = require('backbone.dirty');\n``` \n\n\n### AMD/Require.js \n```javascript\nrequire.config({\n  paths: {\n    \"jquery\"         : \"path/to/jquery/jquery\",\n    \"underscore\"     : \"path/to/underscore/underscore\",\n    \"backbone\"       : \"path/to/backbone/backbone\",\n    \"backbone.dirty\" : \"path/to/backbone.dirty/backbone.dirty\"\n  },\n  shim: {\n    \"underscore\": {\n      exports: \"_\"\n    },\n    \"backbone\": {\n      deps: [\"underscore\", \"jquery\"],\n      exports: \"Backbone\"\n    }\n  }\n});\n```\n\nAnd backbone.dirty can be required with:\n\n```javascript\ndefine(['backbone.dirty'], function(DirtyModel) {\n  var MyModel = DirtyModel.extend({}); \n\n  return MyModel;\n});\n```\n\n## Usage\n\nChange your models to extend from `Backbone.DirtyModel`, e.g.\n\n```javascript\n    var Person = Backbone.Model.extend({ ... });\n    \n    // becomes\n    \n    var Person = Backbone.DirtyModel.extend({ ... });\n``` \n\nIts also possible to use another base model to extend from other than ```Backbone.Model``` \n\nusing ```Backbone.DirtyModel.factory```.\n\nThis enables backbone.dirty to be used with models/model libraries which overrides set or sync.\n\nFor instance Backbone-relational.\n\n```javascript\n  var Person = Backbone.DirtyModel.factory(Backbone.RelationalModel).extend({ ... });\n``` \n\nCalling ```set``` will call Backbone.RelationalModel set before updating dirty state of the model. \n\n## API\n\n### isDirty()\nReturns if the model is dirty or not. Calling ```save``` will reset the dirty state\non success.\n```javascript\nvar person = new Backbone.DirtyModel({ name: 'Bob' });\n\nperson.isDirty(); // false \n\nperson.set('name', 'Bill'); \n\nperson.isDirty(); // true \nperson.save().then(function() { person.isDirty(); /* false */ });\n```\n\n### rollback(object | model, options)\nRollbacks the model to its 'clean' state. Options is the same\nas the options which can be provided to a regular ```set```\n\nIts also possible to rollback using an object. This sets the properties of\nthe provided object as well as marking the model as clean. \n```javascript\n\nvar person = new Backbone.DirtyModel({ name: 'Bob'})\n\nperson.set('name', 'Bill');\nperson.rollback(null, { silent: true }); // don't trigger a change\nperson.get('name'); // returns 'Bob'\n\n// or \n\nperson.rollback({ name: 'Susie' });\nperson.isDirty(); // false\nperson.get('name'); // returns 'Susie'\n\n```\n\n### resetDirty()\n\nMarks the model as clean although the stored attributes which are used in a rollback\nis unchanged\n\n```javascript\n  var person = new Backbone.DirtyModel({ name: 'Bob'})\n\n  person.set('name', 'Bill');\n  person.isDirty(); // true \n  person.resetDirty();\n\n  person.isDirty(); //false\n\n  person.rollback();\n\n  person.get('name'); // Bob\n```\n\n## Events\n\n### \"dirty\"\n\nWhen the model have been dirtied model will trigger a ```dirty``` event.\n\nThis can for instance be used to enable save buttons in views when the model is \"unsaved\"\n\n### \"clean\"\n\nThe opposite of dirty (dah), ```clean``` is triggered on ```rollback```, ```sync``` and by calling ```resetDirty```\n\n\n## Nested models/collections\n\nBackbone.dirty only does a shallow check and will ignore any nested models and collections.\n\nIt's however easy to enable deeper level of dirty checking by letting nested models extend from ```Backbone.DirtyModel``` \n\n### Nested Model\n```\nvar Job = Backbone.DirtyModel.extend({ })\n\nvar Person = Backbone.DirtyModel.extend({ \n  isDirty: function() {\n    return this.get('job').isDirty() \u0026\u0026 Backbone.DirtyModel.prototype.isDirty.call(this);\n  }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrowzy%2Fbackbone.dirty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrowzy%2Fbackbone.dirty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrowzy%2Fbackbone.dirty/lists"}