{"_id":"angular-bindonce","_rev":"7-84105a94da0f119fb37c724bc22096c1","name":"angular-bindonce","description":"Zero watchers binding directives for AngularJS","dist-tags":{"latest":"0.3.1"},"versions":{"0.2.2":{"name":"angular-bindonce","version":"0.2.2","main":"bindonce.js","description":"Zero watchers binding directives for AngularJS","homepage":"https://github.com/Pasvaz/bindonce","author":{"name":"Pasquale Vazzana","email":"pasqualevazzana@gmail.com"},"repository":{"type":"git","url":"https://github.com/Pasvaz/bindonce.git"},"license":"MIT","ignore":["**/.*","node_modules","components"],"dependencies":{"angular":">=1.0.6"},"keywords":["angularjs","angular","directive","binding","watcher","bindonce"],"bugs":{"url":"https://github.com/Pasvaz/bindonce/issues"},"_id":"angular-bindonce@0.2.2","dist":{"shasum":"cd5d4b5f89df0c50d0832e1a3aa0fad17fd21abb","tarball":"https://registry.npmjs.org/angular-bindonce/-/angular-bindonce-0.2.2.tgz","integrity":"sha512-zjcUREdlfJ8ojipYEzWhMY9/mQiBtyvurnjn/cnm34WfVxlMZudokNoTvYfn77B5iYpARRkBm6rgSR0y1Jd3sg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCSQxCl30xV1Lj74dXKUCPNVLaq6w6MqotxKfJs2XjrTAIgMaze9KzD174jFZWNApw3BM8/mbpAnUyNwI5xsFYlVW8="}]},"_from":"angular-bindonce","_npmVersion":"1.3.5","_npmUser":{"name":"pasvaz","email":"pvazzana@medialeader.it"},"maintainers":[{"name":"pasvaz","email":"pvazzana@medialeader.it"}]},"0.3.1":{"name":"angular-bindonce","version":"0.3.1","main":"bindonce.js","description":"Zero watchers binding directives for AngularJS","homepage":"https://github.com/Pasvaz/bindonce","author":{"name":"Pasquale Vazzana","email":"pasqualevazzana@gmail.com"},"repository":{"type":"git","url":"https://github.com/Pasvaz/bindonce.git"},"license":"MIT","ignore":["**/.*","node_modules","components"],"dependencies":{},"keywords":["angularjs","angular","directive","binding","watcher","bindonce"],"bugs":{"url":"https://github.com/Pasvaz/bindonce/issues"},"_id":"angular-bindonce@0.3.1","dist":{"shasum":"af19574abd43f608b9236a302cc5ce49d71dc9c6","tarball":"https://registry.npmjs.org/angular-bindonce/-/angular-bindonce-0.3.1.tgz","integrity":"sha512-lJAyPN4Q6pThbjxSWrb7Mz01yBw2+hjnlhj5U5pgZCdA29rWpHiDU6lRQYC7OrcW5Ovn8dZha7YNItCx0REG1w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDoBk430yOmto0qjR2QoCqx+HvP3Xwxabm/eqbYfUFYbAiEA7jszL7vLydnVxB98lULh9FTfY6enUuRV6lujCG3d1Ko="}]},"_from":".","_npmVersion":"1.3.21","_npmUser":{"name":"pasvaz","email":"pvazzana@medialeader.it"},"maintainers":[{"name":"pasvaz","email":"pvazzana@medialeader.it"}]}},"readme":"Bindonce\r\n========\r\n\r\nHigh performance binding for AngularJs\r\n\r\n## Usage\r\n* download, clone or fork it or install it using [bower](http://twitter.github.com/bower/) `bower install angular-bindonce`\r\n* Include the `bindonce.js` script provided by this component into your app.\r\n* Add `'pasvaz.bindonce'` as a module dependency to your app: `angular.module('app', ['pasvaz.bindonce'])`\r\n\r\n## Overview\r\nAngularJs provides a great data binding system but if you abuse of it the page can run into some performance issues, it's known that more of 2000 watchers can lag the UI and that amount can be reached easily if you don't pay attention to the data-binding. Sometime you really need to bind your data using watchers, especially for SPA because the data are updated in real time, but often you can avoid it with some efforts, most of the data presented in your page, once rendered, are immutable so you shouldn't keep watching them for changes.\r\n\r\nFor instance, take a look to this snippet:\r\n```html\r\n<ul>\r\n\t<li ng-repeat=\"person in Persons\">\r\n\t\t<a ng-href=\"#/people/{{person.id}}\"><img ng-src=\"{{person.imageUrl}}\"></a>\r\n\t\t<a ng-href=\"#/people/{{person.id}}\"><span ng-bind=\"person.name\"></span></a>\r\n\t\t<p ng-class=\"{'cycled':person.generated}\" ng-bind-html-unsafe=\"person.description\"></p>\r\n\t</li>\r\n</ul>\r\n```\r\nAngular internally creates a `$watch` for each `ng-*` directive in order to keep the data up to date, so in this example just for displaying few info it creates 6 + 1 *(ngRepeatWatch)* watchers per `person`, even if the `person` is supposed to remain the same once shown. Iterate this amount for each person and you can have an idea about how easy is to reach 2000 watchers. Now if you need it because those data could change while you show the page or are bound to some models, it's ok. But most of the time they are static data that don't change once rendered. This is where **bindonce** can really help you.\r\n\r\nThe above example done with **bindonce**:\r\n```html\r\n<ul>\r\n\t<li bindonce ng-repeat=\"person in Persons\">\r\n\t\t<a bo-href=\"'#/people/' + person.id\"><img bo-src=\"person.imageUrl\"></a>\r\n\t\t<a bo-href=\"'#/people/' + person.id\" bo-text=\"person.name\"></a>\r\n\t\t<p bo-class=\"{'cycled':person.generated}\" bo-html=\"person.description\"></p>\r\n\t</li>\r\n</ul>\r\n```\r\nNow this example uses **0 watches** per `person` and renders exactly the same result as the above that uses ng-*. *(Angular still uses 1 watcher for ngRepeatWatch)*\r\n\r\n### The smart approach\r\nOK until here nothing completely new, with a bit of efforts you could create your own directive and render the `person` inside the `link` function, or you could use [watch fighters](https://github.com/abourget/abourget-angular) that has a similar approach, but there is still one problem that you have to face and **bindonce** already handles it: *the existence of the data when the directive renders the content*. Usually the directives, unless you use watchers or bind their attributes to the scope (still a watcher), render the content when they are loaded into the markup, but if at that given time your data is not available, the directive can't render it. Bindonce can wait until the data is ready before to rendering the content.\r\nLet's take a look at the follow snippet to better understand the concept:\r\n```html\r\n<span my-custom-set-text=\"Person.firstname\"></span>\r\n<span my-custom-set-text=\"Person.lastname\"></span>\r\n...\r\n<script>\r\nangular.module('testApp', [])\r\n.directive('myCustomSetText', function () {\r\n\treturn {\r\n\t\tlink:function (scope, elem, attr, ctrl) {\r\n\t\t\telem.text(scope.$eval(attr.myCustomSetText));\r\n\t\t}\r\n\t}\r\n});\r\n</script>\r\n```\r\nThis basic directive works as expected, it renders the `Person` data without using any watchers. However, if `Person` is not yet available inside the $scope when the page is loaded (say we get `Person` via $http or via $resource), the directive is useless, `scope.$eval(attr.myCustomSetText)` simply renders nothing and exits.\r\n\r\nHere is how we can solve this issue with **bindonce**:\r\n```html\r\n<div bindonce=\"Person\" bo-title=\"Person.title\">\r\n\t<span bo-text=\"Person.firstname\"></span>\r\n\t<span bo-text=\"Person.lastname\"></span>\r\n\t<img bo-src=\"Person.picture\" bo-alt=\"Person.title\">\r\n\t<p bo-class=\"{'fancy':Person.isNice}\" bo-html=\"Person.story\"></p>\r\n</div>\r\n```\r\n`bindonce=\"Person\"` does the trick, any `bo-*` attribute belonging to `bindonce` waits until the parent `bindonce=\"{somedata}\"` is validated and then renders its content. Once the scope contains the value `Person` then each bo-* child gets filled with the proper values. In order to accomplish this task, **bindonce** uses just **one** temporary watcher, no matters how many children need to be rendered. As soon as it gets `Person` the watcher is promptly removed. If the $scope already contains the data `bindonce` is looking for, then it doesn't create the temporary watcher and simply starts rendering its children.\r\n\r\nYou may have noticed that the first example didn't assign any value to the `bindonce` attribute:\r\n```html\r\n<ul>\r\n\t<li bindonce ng-repeat=\"person in Persons\">\r\n\t...\r\n```\r\nwhen used with `ng-repeat` `bindonce` doesn't need to check if `person` is defined because `ng-repeat` creates the directives only when `person` exists. You could be more explicit: `<li bindonce=\"person\" ng-repeat=\"person in Persons\">`, however assigning a value to `bindonce` in an `ng-repeat` won't make any difference.\r\n\r\n### Interpolation\r\nSome directives (ng-href, ng-src) use interpolation, ie: `ng-href=\"/profile/{{User.profileId}}\"`.\r\nBoth `ng-href` and `ng-src` have the bo-* equivalent directives: `bo-href-i` and `bo-src-i` (pay attention to the **-i**, it stands for **interpolate**). As expected they don't use watchers however Angular creates one watcher per  interpolation, for instance `bo-href-i=\"/profile/{{User.profileId}}\"` sets the element's href **once**, as expected, but Angular keeps a watcher active on `{{User.profileId}}` even if `bo-href-i` doesn't use it.\r\nThat's why by default the `bo-href` doesn't use interpolation or watchers. The above equivalent with 0 watchers would be `bo-href=\"'/profile/' + User.profileId\"`. Nevertheless, `bo-href-i` and `bo-src-i` are still maintained for compatibility reasons.\r\n\r\n### Filters\r\nAlmost every `bo-*` directive replace the equivalent `ng-*` and works in the same ways, except it is evaluated once. \r\nConsequentially you can use any valid angular expression, including filters. This is an example how to use a filter:\r\n```html\r\n<div bindonce=\"Person\">\r\n\t<span bo-bind=\"Person.bill | currency:'USD$'\"></span>\r\n</div>\r\n```\r\n\r\n## Attribute Usage\r\n| Directive  | \tDescription | \tExample  |\r\n|------------|----------------|-----|\r\n| `bindonce=\"{somedata}\"`| **bindonce** is the main directive. `{somedata}` is optional, and if present, forces bindonce to wait until `somedata` is defined before rendering its children  | `<div bindonce=\"Person\">...<div>` |\r\n| `bo-if = \"condition\"`     | equivalent to `ng-if` but doesn't use watchers |`<ANY bo-if=\"Person.isPublic\"></ANY>`|\r\n| `bo-switch = \"expression\"`     | equivalent to `ng-switch` but doesn't use watchers |`<div bo-switch=\"Person.isPublic\">` `<span bo-switch-when=\"'yes\">public</span>` `<span bo-switch-default>private</span>` `</div>`|\r\n| `bo-show = \"condition\"`     | equivalent to `ng-show` but doesn't use watchers |`<ANY bo-show=\"Person.isPublic\"></ANY>`|\r\n| `bo-hide = \"condition\"`     | equivalent to `ng-hide` but doesn't use watchers |`<ANY bo-hide=\"Person.isPrivate\"></ANY>`|\r\n| `bo-text = \"text\"`      | evaluates \"text\" and print it as text inside the element | `<span bo-text=\"Person.name\"></span>` |\r\n| `bo-bind = \"text\"`      | alias for `bo-text`, equivalent to `ng-bind` but doesn't use watchers | `<span bo-bind=\"Person.name\"></span>` |\r\n| `bo-html = \"markup\"`      | evaluates \"markup\" and render it as html inside the element |`bo-html=\"Person.description\"`|\r\n| `bo-href-i = \"url\"`<br>*use `bo-href` instead* | **equivalent** to `ng-href`.<br>**Heads up!** Using interpolation `{{}}` it creates one watcher: <br>`bo-href-i=\"/p/{{Person.id}}\"`. <br>Use `bo-href` to avoid the watcher:<br> `bo-href=\"'/p/' + Person.id\"` |`<a bo-href-i=\"/profile{{Person.id}}\"></a>`|\r\n| `bo-href = \"url\"`      | **similar** to `ng-href` but doesn't allow interpolation using `{{}}` like `ng-href`. <br>**Heads up!** You can't use interpolation `{{}}` inside the url, use bo-href-i for that purpose |`<a bo-href=\"'/profile' + Person.id\"></a>` <br />or<br /> `<a bo-href=\"link\" bo-text=\"Link\"></a>`|\r\n| `bo-src-i = \"url\"`<br>*use `bo-src` instead* | **equivalent** to `ng-src`. <br>**Heads up!** It creates one watcher |`<img bo-src-i=\"{{picture}}\" bo-alt=\"title\">`|\r\n| `bo-src = \"url\"`      | **similar** to `ng-src` but doesn't allow interpolation using `{{}}` like `ng-src`. <br>**Heads up!** You can't use interpolation `{{}}`, use bo-src-i for that purpose |`<img bo-src=\"picture\" bo-alt=\"title\">`|\r\n| `bo-class = \"object/string\"`      | equivalent to `ng-class` but doesn't use watchers |`<span bo-class=\"{'fancy':Person.condition}\">`|\r\n| `bo-alt = \"text\"`      | evaluates \"text\" and render it as `alt` for the element |`<ANY bo-alt=\"title\">`|\r\n| `bo-title = \"text\"`      | evaluates \"text\" and render it as `title` for the element |`<ANY bo-title=\"title\">`|\r\n| `bo-id = \"#id\"`      | evaluates \"#id\" and render it as `id` for the element |`<ANY bo-id=\"id\">`|\r\n| `bo-style = \"object\"`      | equivalent to `ng-style` but doesn't use watchers |`<ANY bo-style=\"{'color':Person.color}\">`|\r\n| `bo-value = \"expression\"`      | evaluates \"expression\" and render it as `value` for the element |`<input type=\"radio\" bo-value=\"value\">`|\r\n| `bo-attr bo-attr-foo = \"text\"`      | evaluates \"text\" and render it as a custom attribute for the element |`<div bo-attr bo-attr-foo=\"bar\"></div>`|\r\n\r\n## Build\r\n```\r\n$ npm install uglify-js -g\r\n$ uglifyjs bindonce.js -c -m -o bindonce.min.js\r\n```\r\n\r\n## Todo\r\nExamples and Tests\r\n\r\n## Copyright\r\nBindOnce was written by **Pasquale Vazzana**, you can follow him on [google+](https://plus.google.com/101872882413388363602) or on [@twitter](https://twitter.com/PasqualeVazzana)\r\n\r\nThanks to all the [contributors](https://github.com/Pasvaz/bindonce/graphs/contributors)\r\n\r\n## LICENSE - \"MIT License\"\r\n\r\nCopyright (c) 2013-2014 Pasquale Vazzana\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in\r\nall copies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r\nTHE SOFTWARE.\r\n","maintainers":[{"name":"pasvaz","email":"pvazzana@medialeader.it"}],"time":{"modified":"2022-06-13T02:50:42.063Z","created":"2013-12-24T01:11:44.299Z","0.2.2":"2013-12-24T01:11:48.671Z","0.3.1":"2014-02-12T14:19:02.209Z"},"author":{"name":"Pasquale Vazzana","email":"pasqualevazzana@gmail.com"},"repository":{"type":"git","url":"https://github.com/Pasvaz/bindonce.git"},"readmeFilename":"README.md"}