Skip to content

Commit 2e02fc2

Browse files
committed
edge -> 0.8 docs
1 parent 099e03c commit 2e02fc2

File tree

119 files changed

+7442
-2
lines changed

Some content is hidden

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

119 files changed

+7442
-2
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function ( grunt ) {
1111

1212
config = {
1313
pkg: grunt.file.readJSON( 'package.json' ),
14-
latest: '0.7',
14+
latest: '0.8',
1515
edge: 'edge',
1616
prod: grunt.option( 'prod' ),
1717

docs/0.8/Adaptors.md.hbs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
Unlike components or other registries where there is a template-level directive that informs Ractive that plugin is to be used, adaptors are a data-level construct and so you use the `adapt` option to tell Ractive which adaptors are to be used with that instance. If you define the adaptors directly on the instance or component, you do not need to specify them in the `adapt` option.
27+
28+
For our example, when you create a new Ractive instance, you can specify which adaptors to use like so:
29+
30+
```js
31+
ractive = new Ractive({
32+
el: container,
33+
template: myTemplate,
34+
data: myBackboneModel,
35+
adapt: [ 'Backbone' ]
36+
});
37+
```
38+
39+
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.
40+
41+
```js
42+
ractive = new Ractive({
43+
el: container,
44+
template: myTemplate,
45+
data: myBackboneModel,
46+
adapt: [ Ractive.adaptors.Backbone ]
47+
});
48+
```
49+
50+
## Creating adaptor plugins
51+
52+
See {{{createLink 'Writing adaptor plugins'}}} to learn how to create your own adaptors.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
title: Advanced Configuration
3+
---
4+
In addition to supplying static values for configurations options, some of the Ractive options
5+
can be specified using a function that will resolve when the ractive instance is instantiated,
6+
and re-evaluated upon {{{createLink 'ractive.reset()'}}}.
7+
8+
An option function may be supplied to either {{{createLink 'Ractive.extend()'}}} or {{{createLink 'new Ractive()'}}}.
9+
10+
Option functions receive the post-configured `data` option as the first argument and `this` in
11+
the function refers to the ractive instance being instantiated.
12+
Bear in mind that the ractive instance is in the process of being configured, so not all options
13+
are necessarily configured yet, and functional methods like `observe` or `set` should not be called.
14+
15+
## Template
16+
17+
The template can be specified using the return value of an option function:
18+
19+
```js
20+
new Ractive({
21+
template: function ( data ) {
22+
return data.foo ? '<p>hello world</p>' : '<div>yes, we have no foo</div>';
23+
},
24+
data: { foo: true }
25+
});
26+
```
27+
The return value can be any of the valid template options: a preparsed template,
28+
a string that will parsed as a template, or an string starting with `#` that refers to an element id from which
29+
the template should be retrieved.
30+
31+
In general, these should be sufficient for most use cases. However, if you need to dissect templates or extract
32+
partials dynamically, the template option function also receives a second argument
33+
with a helper parser object `p` that offers the following methods:
34+
35+
> ### p.fromId( id )
36+
> Retrieves the template from the DOM `<script>` tag specified by `id`. Leading `#` is optional. Make sure to set `type='text/ractive'` on the `<script>` tag or the browser will try and execute the contents as javascript and you'll get an exception.
37+
38+
> ### p.isParsed( template )
39+
> Test whether the supplied instance has already been parsed
40+
41+
> ### p.parse( template [, parseOptions ] )
42+
> Parses the template via {{{createLink 'Ractive.parse()' }}}. If no `parseOptions` specified, defaults to those
43+
> of the current instance. Full Ractive runtime must be loaded.
44+
45+
During a {{{createLink 'ractive.reset()' }}} operation, an option function for a template will be re-evaluated
46+
and _if_ the return value changes the ractive instance will be re-rendered.
47+
48+
## Partials
49+
50+
The value for a named partial can also be specified using a function:
51+
52+
```js
53+
new Ractive({
54+
template: '<div>\{{>dynamicPartial}}</div>',
55+
partials: {
56+
dynamicPartial: function ( data ) {
57+
return data.condition ? '<p>hello world</p>' : '<div>yes, we have no foo</div>';
58+
}
59+
}
60+
});
61+
```
62+
63+
Partial option functions also received the helper parser `p` as the second argument. This is useful in
64+
partials as you cannot return an element id from a partial function and must use `p.fromId` to return
65+
the template content of an element.
66+
67+
Partial functions also cannot directly return the string token of a registered partial. Instead,
68+
return the value from the ractive instance:
69+
70+
```js
71+
partials: {
72+
dynamicPartial: function ( data ) {
73+
// assuming data.partial is the string token of a registered partial:
74+
return this.partials[data.partial];
75+
}
76+
}
77+
```
78+
79+
## Components
80+
81+
A component value can be specified using an option function. The return value can either be
82+
a component or (unlike partials) the string token of a registered component:
83+
84+
```js
85+
Ractive.components.view1 = Ractive.extend({...});
86+
Ractive.components.view2 = Ractive.extend({...});
87+
88+
new Ractive({
89+
template: '<div><dynamicComponent/></div>',
90+
components: {
91+
dynamicComponent: function ( data ) {
92+
return data.foo ? 'view1' : 'view2';
93+
}
94+
}
95+
});
96+
```
97+
98+
## Data
99+
100+
The data option function can either return a value or use the prototype inheritance chain to construct the
101+
data object. Use `this._super` to call the parent data option. Ractive will handle integrating
102+
static data options and data option functions. If a return value is specified, further parent data options
103+
will not be considered.
104+
105+
```js
106+
107+
var Component1 = Ractive.extend({
108+
data: {
109+
formatTitle: function (title) {
110+
return '"' + title.toUpperCase() + '"';
111+
}
112+
}
113+
});
114+
115+
var Component2 = Component1.extend({
116+
data: function( data ) {
117+
this._super( data );
118+
data.scale = 5;
119+
}
120+
});
121+
122+
var ractive = new Component2({
123+
data: { foo: 'bar' }
124+
})
125+
126+
// r.data: { foo: "bar", formatTitle: function, scale: 5 }
127+
128+
```
129+
130+
The data object instance passed to the instantiated ractive instance will always be retained as
131+
the `ractive.data` instance, _unless_ a return value is specified from an option function in which
132+
case that return value instance will be used as `ractive.data`
133+
134+
### Copying Parent Data
135+
136+
Because parent data is common to all instances, you can use an option function to return a
137+
unique copy of the data.
138+
139+
```js
140+
141+
var Shared = Ractive.extend({
142+
data: {
143+
foo: { bar: 42 }
144+
}
145+
});
146+
147+
var shared1 = new Shared();
148+
var shared2 = new Shared();
149+
shared1.set( 'foo.bar', 12 );
150+
shared2.get( 'foo.bar' ); // returns 12
151+
152+
153+
var NotShared = Ractive.extend({
154+
data: function () {
155+
return {
156+
foo: { bar: 42 }
157+
};
158+
}
159+
});
160+
161+
var notShared1 = new NotShared();
162+
var notShared2 = new NotShared();
163+
notShared1.set( 'foo.bar', 12 );
164+
notShared2.get( 'foo.bar' ); // returns 42
165+
166+
```
167+
168+
### Asynchronous Data
169+
170+
A data option function can be a handy way to fetch asynchronous data _and_ supply initial synchronous values:
171+
172+
```js
173+
data: function () {
174+
175+
$.get( 'somedata.url', function( data ) {
176+
this.set( '', data );
177+
}.bind(this) );
178+
179+
return {
180+
foo: 'default'
181+
};
182+
}
183+
```
184+
185+
### Specifying a Model
186+
187+
Another use of the data option function is to provide a model:
188+
189+
```js
190+
data: function ( data ) {
191+
return new Model( data );
192+
}
193+
```
194+
195+
If you use a constructor guard clause (currently popular for `new`-less use of javascript constructors),
196+
you can directly supply the model:
197+
198+
199+
```js
200+
function Model ( data ) {
201+
if ( !( this instanceof Model) ) { return new Model( data ); }
202+
// model setup
203+
}
204+
205+
var MyComponent = Ractive.extend({
206+
data: Model
207+
});
208+
209+
var r = new MyComponent({
210+
data: { foo: 'bar' }
211+
})
212+
```

docs/0.8/Array modification.md.hbs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Array modification
3+
---
4+
Ractive can 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 enable this behaviour by passing in `modifyArrays: true` as an {{{createLink 'options' '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.

docs/0.8/CSP.md.hbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Content Security Policy
3+
---
4+
5+
To use ractive with [Content Security Policy](http://www.html5rocks.com/en/tutorials/security/content-security-policy/), you'll currently need `'unsafe-eval'` specified for `scriptSrc` in your CSP header.
6+
7+
This may change in future - see https://github.com/ractivejs/ractive/issues/1897 .
8+

0 commit comments

Comments
 (0)