Use `rivets.configure` to configure Rivets.js for your app (or you can set configuration options manually on `rivets.config`).
...
...
@@ -40,7 +43,8 @@ Use `rivets.configure` to configure Rivets.js for your app (or you can set confi
Rivets.js is model interface-agnostic, meaning it can work with any event-driven model by way of defining an adapter. This is the only required configuration as it's what Rivet.js uses to observe and interact with your model objects. An adapter is just an object that responds to `subscribe`, `unsubscribe`, `read` and `publish`. Here is a sample configuration with an adapter for using Rivets.js with Backbone.js.
rivets.configure({
```javascript
rivets.configure({
adapter:{
subscribe:function(obj,keypath,callback){
callback.wrapped=function(m,v){callback(v)};
...
...
@@ -56,22 +60,26 @@ Rivets.js is model interface-agnostic, meaning it can work with any event-driven
obj.set(keypath,value);
}
}
});
});
```
#### Prefix and data preloading
To prevent data attribute collision, you can set the `prefix` option to something like 'rv' or 'bind' so that data attributes are prefixed like `data-rv-text`.
rivets.configure({
```javascript
rivets.configure({
prefix:'rv'
});
});
```
Set the `preloadData` option to `false` if you don't want your bindings to be bootstrapped with the current model values on bind. This option is set to `true` by default.
rivets.configure({
```javascript
rivets.configure({
preloadData:false
});
});
```
## Extend
...
...
@@ -83,13 +91,17 @@ Rivets.js is easily extended by adding your own custom *binding routines* and *f
Let's say we wanted a `data-color` binding that sets the element's colour, here's what the routine function for that binding might look like:
rivets.routines.color = function(el, value) {
```javascript
rivets.routines.color=function(el,value){
el.style.color=value;
};
};
```
With that routine defined, the following binding will update the element's color when `model.color` changes:
<span data-color="model.color">COLOR</span>
```html
<spandata-color="model.color">COLOR</span>
```
Available bindings out-of-the-box:
...
...
@@ -111,7 +123,7 @@ Available bindings out-of-the-box:
*Formatters* are simple one-way functions that mutate the incoming value of a binding. You can use them to format dates, numbers, currencies, etc. and because they work in a similar fashion to the Unix pipeline, the output of each feeds directly as input to the next one, so you can stack as many of them together as you like.
@@ -141,13 +153,15 @@ Just use `model:property` instead of `model.property` inside your binding declar
Computed properties are functions that get re-evaluated when one or more dependent properties change. Declaring computed properties in Rivets.js is simple, just separate the function from it's dependencies with a *<*. The following `data-text` binding will get re-evaluated with `event.duration()` when either the event's `start` or `end` attribute changes.
Use the `data-each-[item]` binding to have Rivets.js automatically loop over items in an array and append bound instances of that element. Within that element you can bind to the iterated item as well as any contexts that are available in the parent view.
```
```html
<ul>
<lidata-each-todo="list.todos">
<inputtype="checkbox"data-checked="todo.done">
...
...
@@ -158,7 +172,7 @@ Use the `data-each-[item]` binding to have Rivets.js automatically loop over ite
If the array you're binding to contains non-model objects (they don't conform to your adapter), you can still iterate over them, just make sure to use the adapter bypass syntax — in doing so, the iteration binding will still update when the array changes, however the individual items will not since they'd be bypassing the `adapter.subscribe`.