Skip to content

Commit 9f0ea8b

Browse files
committed
initial inclusion of edge docs
1 parent f63931b commit 9f0ea8b

File tree

87 files changed

+4856
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+4856
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
node_modules
33
tmp
44
build
5-
.sass-cache
5+
.sass-cache
6+
copy

Gruntfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = function ( grunt ) {
1212
config = {
1313
pkg: grunt.file.readJSON( 'package.json' ),
1414
latest: '0.4.0',
15+
edge: '0.4.1',
1516
prod: grunt.option( 'prod' ),
1617

1718
// TODO do we need this?... probably not, it just got

docs/0.4.1/Adaptors.md.hbs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Adaptors
3+
---
4+
5+
Adaptors are a way of teaching Ractive to communicate seamlessly with other libraries such as Backbone. This means that you can, for example, have some or all of your app's data handled by Backbone - including fetching from and saving to your server, validation, sorting, filtering and so on - but still build a reactive user interface using Ractive, without having to create custom View classes or adding a whole load of event binding code.
6+
7+
It's probably easier to show, rather than tell: [this example application](http://examples.ractivejs.org/backbone) uses Backbone models describing all the James Bond films. The [code for the Backbone adaptor is here](https://github.com/ractivejs/ractive-adaptors-backbone).
8+
9+
10+
Using adaptors
11+
--------------
12+
13+
Add the adaptor code to your app. Using the Backbone adaptor as an example:
14+
15+
```html
16+
<script src='lib/underscore.js'></script> <!-- Backbone dependency -->
17+
<script src='lib/backbone.js'></script>
18+
<script src='lib/ractive.js'></script>
19+
20+
<!-- the adaptor -->
21+
<script src='lib/adaptors/ractive-adaptors-backbone.js'></script>
22+
```
23+
24+
If you're using module loaders, beware - the adaptor needs access to both `ractive` and `backbone`. You may need to change your paths config (or equivalent), or modify the adaptor source code to fit your app.
25+
26+
When you create a new Ractive instance, you can specify which adaptors to use like so:
27+
28+
```js
29+
ractive = new Ractive({
30+
el: container,
31+
template: myTemplate,
32+
data: myBackboneModel,
33+
adapt: [ 'Backbone' ]
34+
});
35+
```
36+
37+
Ractive will then see if there's a `Backbone` property of `Ractive.adaptors`. (If not, an error will be thrown.) Alternatively, you can pass in the adaptor itself rather than the name, e.g.
38+
39+
```js
40+
ractive = new Ractive({
41+
el: container,
42+
template: myTemplate,
43+
data: myBackboneModel,
44+
adapt: [ Ractive.adaptors.Backbone ]
45+
});
46+
```
47+
48+
## Creating adaptor plugins
49+
50+
See {{{createLink 'Writing adaptor plugins'}}} to learn how to create your own adaptors.

docs/0.4.1/Array modification.md.hbs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Array modification
3+
---
4+
By default, Ractive will intercept the *mutator methods* (`pop`, `push`, `shift`, `unshift`, `splice`, `sort` and `reverse`) of arrays that it [depends on](dependants) for more convenient data binding.
5+
6+
Consider the following:
7+
8+
```html
9+
<ul>
10+
\{{#list}}
11+
<li>\{{this}}</li>
12+
\{{/list}}
13+
</ul>
14+
```
15+
16+
```js
17+
list = [ 'a', 'b', 'c' ];
18+
19+
ractive = new Ractive({
20+
el: myContainer,
21+
template: myTemplate,
22+
data: { list: list }
23+
});
24+
25+
list.push( 'd' ); // adds a new list item - <li>d</li>
26+
```
27+
28+
You can disable this behaviour by passing in `modifyArrays: false` as an {{{createLink 'initialisation Options'}}}
29+
30+
31+
## How it works
32+
33+
Don't worry, we're not modifying `Array.prototype`. (What do you think this is, [Ember](http://emberjs.com/guides/configuring-ember/disabling-prototype-extensions/)? :-)
34+
35+
Instead, we're using a technique called [prototype chain injection](http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/#wrappers_prototype_chain_injection), which allows us to remain performant and memory-efficient without mucking about extending native objects.
36+
37+
This uses the non-standard (but very unlikely to disappear!) `__proto__` property. That might seem kludgy, but if [Mike Bostock thinks it's okay](http://bost.ocks.org/mike/selection/#subclass) then that's good enough for us.
38+
39+
Older browsers (I'm looking at you, IE8) don't support `__proto__` - in these cases, we simply add the wrapped methods as properties of the array itself.
40+
41+
As well as intercepting or wrapping the mutator methods, Ractive adds a (non-enumerable, in modern browsers) `_ractive` property to arrays, which contains information about which Ractive instances depend on the array, and which keypaths it is assigned to.
42+
43+
44+
## Hygiene
45+
46+
When an array is no longer depended on by any Ractive instances, we can revert it to its normal state - resetting its prototype (if we used prototype chain injection) or deleting the wrapped methods (if we're in a crap browser), and removing the `_ractive` property.
47+
48+
49+
## Performance and UI benefits
50+
51+
As well as convenience, using arrays like this helps Ractive make smart decisions about how to update the DOM. Continuing the example above, compare these two alternative methods of inserting a new item at the *start* of our list:
52+
53+
```js
54+
// at the moment, list = [ 'a', 'b', 'c', 'd' ]
55+
56+
// 1. Reset the list:
57+
ractive.set( 'list', [ 'z', 'a', 'b', 'c', 'd' ] )
58+
59+
// 2. Use `unshift`:
60+
list.unshift( 'z' );
61+
```
62+
63+
In the first example, Ractive will see that the content of the first list item has changed from `'a'` to `'z'`, and that the second has changed from `'b'` to `'a'`, and so on, and update the DOM accordingly. It will also see that there is now a fifth item, so will append `<li>d</li>` to the list.
64+
65+
In the second example, Ractive will understand that all it needs to do is insert `<li>z</li>` at the start of the list, leaving everything else untouched.
66+
67+
This is particularly important if you're using {{{createLink 'transitions'}}}, as it will be obvious to the user which elements are being added and removed.
68+
69+
Note that if `list.unshift('z')` isn't an option, you could use {{{createLink 'ractive.merge()'}}} to achieve the same effect.

0 commit comments

Comments
 (0)