d5ccb916 by Michael Richards

Add language identifiers to code blocks so that GitHub will syntax highlight them. [Closes #60]

1 parent e0b6c36b
Showing 1 changed file with 34 additions and 20 deletions
...@@ -6,7 +6,8 @@ Rivets.js is a declarative data binding facility that plays well with existing f ...@@ -6,7 +6,8 @@ Rivets.js is a declarative data binding facility that plays well with existing f
6 6
7 Describe your UI in plain HTML using data attributes: 7 Describe your UI in plain HTML using data attributes:
8 8
9 <div id='auction'> 9 ```html
10 <div id='auction'>
10 <h1 data-text='auction.title'></h1> 11 <h1 data-text='auction.title'></h1>
11 <img data-src='auction.image_url'> 12 <img data-src='auction.image_url'>
12 <span data-text='auction.timeRemaining | time'></span> 13 <span data-text='auction.timeRemaining | time'></span>
...@@ -26,12 +27,14 @@ Describe your UI in plain HTML using data attributes: ...@@ -26,12 +27,14 @@ Describe your UI in plain HTML using data attributes:
26 <dt>Bids Left:</dt> 27 <dt>Bids Left:</dt>
27 <dd data-text='user.bidCount'></dd> 28 <dd data-text='user.bidCount'></dd>
28 </dl> 29 </dl>
29 </div> 30 </div>
31 ```
30 32
31 Then tell Rivets.js what model(s) to bind to it: 33 Then tell Rivets.js what model(s) to bind to it:
32 34
33 rivets.bind($('#auction'), {auction: auction, user: currentUser}); 35 ```javascript
34 36 rivets.bind($('#auction'), {auction: auction, user: currentUser});
37 ```
35 ## Configure 38 ## Configure
36 39
37 Use `rivets.configure` to configure Rivets.js for your app (or you can set configuration options manually on `rivets.config`). 40 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 ...@@ -40,7 +43,8 @@ Use `rivets.configure` to configure Rivets.js for your app (or you can set confi
40 43
41 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. 44 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.
42 45
43 rivets.configure({ 46 ```javascript
47 rivets.configure({
44 adapter: { 48 adapter: {
45 subscribe: function(obj, keypath, callback) { 49 subscribe: function(obj, keypath, callback) {
46 callback.wrapped = function(m, v) { callback(v) }; 50 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 ...@@ -56,22 +60,26 @@ Rivets.js is model interface-agnostic, meaning it can work with any event-driven
56 obj.set(keypath, value); 60 obj.set(keypath, value);
57 } 61 }
58 } 62 }
59 }); 63 });
60 64 ```
61 65
62 #### Prefix and data preloading 66 #### Prefix and data preloading
63 67
64 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`. 68 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`.
65 69
66 rivets.configure({ 70 ```javascript
71 rivets.configure({
67 prefix: 'rv' 72 prefix: 'rv'
68 }); 73 });
74 ```
69 75
70 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. 76 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.
71 77
72 rivets.configure({ 78 ```javascript
79 rivets.configure({
73 preloadData: false 80 preloadData: false
74 }); 81 });
82 ```
75 83
76 ## Extend 84 ## Extend
77 85
...@@ -83,13 +91,17 @@ Rivets.js is easily extended by adding your own custom *binding routines* and *f ...@@ -83,13 +91,17 @@ Rivets.js is easily extended by adding your own custom *binding routines* and *f
83 91
84 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: 92 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:
85 93
86 rivets.routines.color = function(el, value) { 94 ```javascript
95 rivets.routines.color = function(el, value) {
87 el.style.color = value; 96 el.style.color = value;
88 }; 97 };
98 ```
89 99
90 With that routine defined, the following binding will update the element's color when `model.color` changes: 100 With that routine defined, the following binding will update the element's color when `model.color` changes:
91 101
92 <span data-color="model.color">COLOR</span> 102 ```html
103 <span data-color="model.color">COLOR</span>
104 ```
93 105
94 Available bindings out-of-the-box: 106 Available bindings out-of-the-box:
95 107
...@@ -111,7 +123,7 @@ Available bindings out-of-the-box: ...@@ -111,7 +123,7 @@ Available bindings out-of-the-box:
111 123
112 *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. 124 *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.
113 125
114 ``` 126 ```javascript
115 rivets.formatters.money = function(value){ 127 rivets.formatters.money = function(value){
116 return accounting.formatMoney(value); 128 return accounting.formatMoney(value);
117 }; 129 };
...@@ -121,7 +133,7 @@ rivets.formatters.date = function(value){ ...@@ -121,7 +133,7 @@ rivets.formatters.date = function(value){
121 }; 133 };
122 ``` 134 ```
123 135
124 ``` 136 ```html
125 <span data-text="event.startDate | date"></span> 137 <span data-text="event.startDate | date"></span>
126 ``` 138 ```
127 139
...@@ -141,13 +153,15 @@ Just use `model:property` instead of `model.property` inside your binding declar ...@@ -141,13 +153,15 @@ Just use `model:property` instead of `model.property` inside your binding declar
141 153
142 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. 154 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.
143 155
144 <span data-text="event:duration < start end"></span> 156 ```html
157 <span data-text="event:duration < start end"></span>
158 ```
145 159
146 #### Iteration Binding 160 #### Iteration Binding
147 161
148 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. 162 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.
149 163
150 ``` 164 ```html
151 <ul> 165 <ul>
152 <li data-each-todo="list.todos"> 166 <li data-each-todo="list.todos">
153 <input type="checkbox" data-checked="todo.done"> 167 <input type="checkbox" data-checked="todo.done">
...@@ -158,7 +172,7 @@ Use the `data-each-[item]` binding to have Rivets.js automatically loop over ite ...@@ -158,7 +172,7 @@ Use the `data-each-[item]` binding to have Rivets.js automatically loop over ite
158 172
159 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`. 173 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`.
160 174
161 ``` 175 ```html
162 <ul> 176 <ul>
163 <li data-each-link="item.links"> 177 <li data-each-link="item.links">
164 <a data-href="link:url" data-text="link:title"></span> 178 <a data-href="link:url" data-text="link:title"></span>
...@@ -168,7 +182,7 @@ If the array you're binding to contains non-model objects (they don't conform to ...@@ -168,7 +182,7 @@ If the array you're binding to contains non-model objects (they don't conform to
168 182
169 Also note that you may bind to the iterated item directly on the parent element. 183 Also note that you may bind to the iterated item directly on the parent element.
170 184
171 ``` 185 ```html
172 <ul> 186 <ul>
173 <li data-each-tag="item.tags" data-text="tag:name"></li> 187 <li data-each-tag="item.tags" data-text="tag:name"></li>
174 </ul> 188 </ul>
......