{"id":21813259,"url":"https://github.com/morficus/backbone.dataevents","last_synced_at":"2025-03-21T09:22:18.576Z","repository":{"id":25045626,"uuid":"28465461","full_name":"morficus/backbone.dataEvents","owner":"morficus","description":"Convenience attributes on your Views, to easily bind to model and collection events","archived":false,"fork":false,"pushed_at":"2014-12-29T07:18:32.000Z","size":356,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-26T06:12:43.470Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/morficus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-25T00:19:32.000Z","updated_at":"2014-12-29T07:04:22.000Z","dependencies_parsed_at":"2022-08-23T15:40:29.975Z","dependency_job_id":null,"html_url":"https://github.com/morficus/backbone.dataEvents","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morficus%2Fbackbone.dataEvents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morficus%2Fbackbone.dataEvents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morficus%2Fbackbone.dataEvents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morficus%2Fbackbone.dataEvents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morficus","download_url":"https://codeload.github.com/morficus/backbone.dataEvents/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244768321,"owners_count":20507229,"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-27T14:28:14.276Z","updated_at":"2025-03-21T09:22:18.557Z","avatar_url":"https://github.com/morficus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Backbone.dataEvents\n[![Build Status](https://travis-ci.org/morficus/backbone.dataEvents.svg?branch=master)](https://travis-ci.org/morficus/backbone.dataEvents)\n[![Code Climate](https://codeclimate.com/github/morficus/backbone.dataEvents/badges/gpa.svg)](https://codeclimate.com/github/morficus/backbone.dataEvents)\n\nInspired by Backbone.Marionette's [modelEvents and collectionEvents](http://marionettejs.com/docs/marionette.view.html#viewmodelevents-and-viewcollectionevents)\n\u003e Similar to the events hash, views can specify a configuration hash for collections and models.\n\n\n## Benefits\n* Better code organization via declarative event bindings for models and collections\n* Lightweight, only 1kb (vs Marionette which is 40kb minified)\n\n## Usage\n\n`bower install backbone.dataevents --save`\n\n### Getting it into your project\nThe Backbone.dataEvents plugin follows the [UMD pattern](https://github.com/umdjs/umd) for JavaScript modules, that means that it can be loaded in a browser via a traditional `\u003cscript\u003e` tag or a module loader (like [require.js](http://requirejs.org/)) or even server-side with Node.js  \n\n\nIf you are using a module loader, just include it as any other module.  \nIf you choose to load it via script tags, just make sure to do so after you include Backbone.  \n\n```html\n \u003cscript type=\"text/javascript\" src=\"underscore.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\" src=\"backbone.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\" src=\"backbone.global.js\"\u003e\u003c/script\u003e\n ```\n\n### In your code\n\nOnce it is loaded, all of your views will have access to 2 new properties (just extend from `Backbone.View` as usual): `modelEvents` and `collectionEvents`.  \n\nThese properties will define the event binding between your view and the events triggered from the model or collection (custom and built-in events). As mentioned at the top of this document, these are both hashes that follow a  format very similar to the regular Backbone.View `events` hash. Here is an example:\n\nWhen binding to multiple model or collection events, you typically write something like this:\n```javascript\nvar MyView = Backbone.View.extend({\n    events: {\n        'click .button.delete': 'destroy',\n        'click .button.add': 'openAddDialog',\n        'click .button.edit': 'openEditDialog'\n    },\n    \n    initialize: function () {\n        this.listenTo(this.collection, 'add', this.insertNewRow);\n        this.listenTo(this.collection, 'add', this.updateCounter);\n        this.listenTo(this.collection, 'remove', this.deleteRow);\n        this.listenTo(this.collection, 'remove', this.updateCounter);\n        this.listenTo(this.collection, 'change', this.render);\n    },\n    \n    render: function () {\n        ...\n    }\n});\n```\n\nThis plugin allows you to write those bindings like this:\n```javascript\nvar MyView = Backbone.View.extend({\n    events: {\n        'click .button.delete': 'destroy',\n        'click .button.add': 'openAddDialog',\n        'click .button.edit': 'openEditDialog'\n    },\n    \n    collectionEvents: {\n        'add': ['insertNewRow', 'updateCounter'],\n        'remove': ['deleteRow', 'updateCounter'],\n        'change': 'render'\n    },\n    \n    initialize: function () {\n        ...\n    },\n    \n    render: function () {\n        ...\n    }\n});\n```\n\nPretty neat, huh?\n\n\nBoth the `modelEvents` and `collectionEvents` support 3 forms to callbacks:\n\n1- named functions:  \n    `'change': 'someFunctionName'`  \n2- anonymous functions:  \n    `'change': function() { /* cool stuff here */ }`  \n3- an array of named functions (most useful when you want to call more than 1 function when an event is triggered):   \n    `'change': ['someFunctionName', 'someOtherFunction`]`  \n\n\n\n## Additional reading\nFor more examples, take a look at the [examples directory](https://github.com/morficus/backbone.dataEvents/tree/master/examples)\n\n\nIf you found this plugin useful, then you may also like [Backbone.global](https://github.com/DarrylD/Backbone.global) which does the same thing but for custom global events. I suggest you check it out as well.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorficus%2Fbackbone.dataevents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorficus%2Fbackbone.dataevents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorficus%2Fbackbone.dataevents/lists"}