Update documents

This commit is contained in:
Jinzhu 2016-03-08 23:15:08 +08:00
parent 87fefe0c35
commit 1089f6be30
22 changed files with 431 additions and 25 deletions

View File

@ -70,7 +70,7 @@
data-chapter-title="Advanced Usage" data-chapter-title="Advanced Usage"
data-filepath="advanced.md" data-filepath="advanced.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -70,7 +70,7 @@
data-chapter-title="Associations" data-chapter-title="Associations"
data-filepath="associations.md" data-filepath="associations.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -70,7 +70,7 @@
data-chapter-title="Callbacks" data-chapter-title="Callbacks"
data-filepath="callbacks.md" data-filepath="callbacks.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -68,7 +68,7 @@
data-chapter-title="Change Log" data-chapter-title="Change Log"
data-filepath="changelog.md" data-filepath="changelog.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -353,7 +353,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -70,7 +70,7 @@
data-chapter-title="CRUD: Reading and Writing Data" data-chapter-title="CRUD: Reading and Writing Data"
data-filepath="curd.md" data-filepath="curd.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>
@ -630,7 +630,10 @@
<ul> <ul>
<li><a href="#create">Create</a><ul> <li><a href="#create">Create</a><ul>
<li><a href="#create-record">Create Record</a></li> <li><a href="#create-record">Create Record</a></li>
<li><a href="#create-with-associations">Create With Associations</a></li> <li><a href="#create-with-associations">Create With Associations</a><ul>
<li><a href="#skip-save-associations-when-creating">Skip Save Associations when creating</a></li>
</ul>
</li>
<li><a href="#default-values">Default Values</a></li> <li><a href="#default-values">Default Values</a></li>
<li><a href="#setting-primary-key-in-callbacks">Setting Primary Key In Callbacks</a></li> <li><a href="#setting-primary-key-in-callbacks">Setting Primary Key In Callbacks</a></li>
<li><a href="#extra-creating-option">Extra Creating option</a></li> <li><a href="#extra-creating-option">Extra Creating option</a></li>
@ -667,6 +670,10 @@
<li><a href="#specifying-the-table-name">Specifying The Table Name</a></li> <li><a href="#specifying-the-table-name">Specifying The Table Name</a></li>
</ul> </ul>
</li> </li>
<li><a href="#preloading-eager-loading">Preloading (Eager loading)</a><ul>
<li><a href="#nested-preloading">Nested Preloading</a></li>
</ul>
</li>
<li><a href="#update">Update</a><ul> <li><a href="#update">Update</a><ul>
<li><a href="#update-all-fields">Update All Fields</a></li> <li><a href="#update-all-fields">Update All Fields</a></li>
<li><a href="#update-changed-fields">Update Changed Fields</a></li> <li><a href="#update-changed-fields">Update Changed Fields</a></li>
@ -675,6 +682,7 @@
<li><a href="#batch-updates">Batch Updates</a></li> <li><a href="#batch-updates">Batch Updates</a></li>
<li><a href="#update-with-sql-expression">Update with SQL Expression</a></li> <li><a href="#update-with-sql-expression">Update with SQL Expression</a></li>
<li><a href="#change-updating-values-in-callbacks">Change Updating Values In Callbacks</a></li> <li><a href="#change-updating-values-in-callbacks">Change Updating Values In Callbacks</a></li>
<li><a href="#skip-save-associations-when-updating">Skip Save Associations when updating</a></li>
<li><a href="#extra-updating-option">Extra Updating option</a></li> <li><a href="#extra-updating-option">Extra Updating option</a></li>
</ul> </ul>
</li> </li>
@ -719,6 +727,10 @@ db.Create(&amp;user)
</code></pre> </code></pre>
<h3 id="create-with-associations">Create With Associations</h3> <h3 id="create-with-associations">Create With Associations</h3>
<p>Refer <a href="associations.html">Associations</a> for more details</p> <p>Refer <a href="associations.html">Associations</a> for more details</p>
<h4 id="skip-save-associations-when-creating">Skip Save Associations when creating</h4>
<p>By default, GORM will save associations also when creating, you could skip it by set <code>gorm:save_associations</code> to <code>false</code></p>
<pre><code class="lang-go">db.Set(<span class="hljs-string">&quot;gorm:save_associations&quot;</span>, <span class="hljs-literal">false</span>).Create(&amp;user)
</code></pre>
<h3 id="default-values">Default Values</h3> <h3 id="default-values">Default Values</h3>
<p>You could define default value in the <code>gorm</code> tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields&apos;s value from database.</p> <p>You could define default value in the <code>gorm</code> tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields&apos;s value from database.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Animal <span class="hljs-keyword">struct</span> { <pre><code class="lang-go"><span class="hljs-keyword">type</span> Animal <span class="hljs-keyword">struct</span> {
@ -1097,6 +1109,29 @@ db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Find(&amp;d
db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Delete() db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Delete()
<span class="hljs-comment">//// DELETE FROM deleted_users WHERE name = &apos;jinzhu&apos;;</span> <span class="hljs-comment">//// DELETE FROM deleted_users WHERE name = &apos;jinzhu&apos;;</span>
</code></pre> </code></pre>
<h2 id="preloading-eager-loading">Preloading (Eager loading)</h2>
<pre><code class="lang-go">db.Preload(<span class="hljs-string">&quot;Orders&quot;</span>).Find(&amp;users)
<span class="hljs-comment">//// SELECT * FROM users;</span>
<span class="hljs-comment">//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);</span>
db.Preload(<span class="hljs-string">&quot;Orders&quot;</span>, <span class="hljs-string">&quot;state NOT IN (?)&quot;</span>, <span class="hljs-string">&quot;cancelled&quot;</span>).Find(&amp;users)
<span class="hljs-comment">//// SELECT * FROM users;</span>
<span class="hljs-comment">//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN (&apos;cancelled&apos;);</span>
db.Where(<span class="hljs-string">&quot;state = ?&quot;</span>, <span class="hljs-string">&quot;active&quot;</span>).Preload(<span class="hljs-string">&quot;Orders&quot;</span>, <span class="hljs-string">&quot;state NOT IN (?)&quot;</span>, <span class="hljs-string">&quot;cancelled&quot;</span>).Find(&amp;users)
<span class="hljs-comment">//// SELECT * FROM users WHERE state = &apos;active&apos;;</span>
<span class="hljs-comment">//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN (&apos;cancelled&apos;);</span>
db.Preload(<span class="hljs-string">&quot;Orders&quot;</span>).Preload(<span class="hljs-string">&quot;Profile&quot;</span>).Preload(<span class="hljs-string">&quot;Role&quot;</span>).Find(&amp;users)
<span class="hljs-comment">//// SELECT * FROM users;</span>
<span class="hljs-comment">//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many</span>
<span class="hljs-comment">//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one</span>
<span class="hljs-comment">//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to</span>
</code></pre>
<h3 id="nested-preloading">Nested Preloading</h3>
<pre><code class="lang-go">db.Preload(<span class="hljs-string">&quot;Orders.OrderItems&quot;</span>).Find(&amp;users)
db.Preload(<span class="hljs-string">&quot;Orders&quot;</span>, <span class="hljs-string">&quot;state = ?&quot;</span>, <span class="hljs-string">&quot;paid&quot;</span>).Preload(<span class="hljs-string">&quot;Orders.OrderItems&quot;</span>).Find(&amp;users)
</code></pre>
<h2 id="update">Update</h2> <h2 id="update">Update</h2>
<h3 id="update-all-fields">Update All Fields</h3> <h3 id="update-all-fields">Update All Fields</h3>
<p><code>Save</code> will include all fields when perform the Updating SQL, even it is not changed</p> <p><code>Save</code> will include all fields when perform the Updating SQL, even it is not changed</p>
@ -1181,6 +1216,10 @@ DB.Model(&amp;product).Where(<span class="hljs-string">&quot;quantity &gt; 1&quo
} }
} }
</code></pre> </code></pre>
<h3 id="skip-save-associations-when-updating">Skip Save Associations when updating</h3>
<p>By default, GORM will save associations also when updating, you could skip it by set <code>gorm:save_associations</code> to <code>false</code></p>
<pre><code class="lang-go">db.Set(<span class="hljs-string">&quot;gorm:save_associations&quot;</span>, <span class="hljs-literal">false</span>).Save(&amp;user)
</code></pre>
<h3 id="extra-updating-option">Extra Updating option</h3> <h3 id="extra-updating-option">Extra Updating option</h3>
<pre><code class="lang-go">// Add extra SQL option for updating SQL <pre><code class="lang-go">// Add extra SQL option for updating SQL
db.Model(&amp;user).Set(&quot;gorm:update_option&quot;, &quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;).Update(&quot;name, &quot;hello&quot;) db.Model(&amp;user).Set(&quot;gorm:update_option&quot;, &quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;).Update(&quot;name, &quot;hello&quot;)

View File

@ -70,7 +70,7 @@
data-chapter-title="Database" data-chapter-title="Database"
data-filepath="database.md" data-filepath="database.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -70,7 +70,7 @@
data-chapter-title="Development" data-chapter-title="Development"
data-filepath="development.md" data-filepath="development.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -17,7 +17,7 @@
* [CRUD: Reading and Writing Data](curd.md) * [CRUD: Reading and Writing Data](curd.md)
* [Create](curd.md#create), * [Create](curd.md#create),
* [Query](curd.md#query), * [Query](curd.md#query),
* [Preloading (Eager Loading)](curd.md#preloading), * [Preloading (Eager Loading)](curd.md#preloading-eager-loading),
* [Update](curd.md#update), * [Update](curd.md#update),
* [Delete / Soft Delete](curd.md#delete) * [Delete / Soft Delete](curd.md#delete)
* [Callbacks](callbacks.md) * [Callbacks](callbacks.md)

View File

@ -42,6 +42,14 @@ db.Create(&user)
Refer [Associations](associations.html) for more details Refer [Associations](associations.html) for more details
#### Skip Save Associations when creating
By default, GORM will save associations also when creating, you could skip it by set `gorm:save_associations` to `false`
```go
db.Set("gorm:save_associations", false).Create(&user)
```
### Default Values ### Default Values
You could define default value in the `gorm` tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields's value from database. You could define default value in the `gorm` tag, then the inserting SQL will ignore these fields that has default value and its value is blank, and after insert the record into databae, gorm will load those fields's value from database.
@ -524,6 +532,35 @@ db.Table("deleted_users").Where("name = ?", "jinzhu").Delete()
//// DELETE FROM deleted_users WHERE name = 'jinzhu'; //// DELETE FROM deleted_users WHERE name = 'jinzhu';
``` ```
## Preloading (Eager loading)
```go
db.Preload("Orders").Find(&users)
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');
db.Where("state = ?", "active").Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
//// SELECT * FROM users WHERE state = 'active';
//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');
db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
//// SELECT * FROM users;
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
```
### Nested Preloading
```go
db.Preload("Orders.OrderItems").Find(&users)
db.Preload("Orders", "state = ?", "paid").Preload("Orders.OrderItems").Find(&users)
```
## Update ## Update
### Update All Fields ### Update All Fields
@ -636,6 +673,14 @@ func (user *User) BeforeSave(scope *gorm.Scope) (err error) {
} }
``` ```
### Skip Save Associations when updating
By default, GORM will save associations also when updating, you could skip it by set `gorm:save_associations` to `false`
```go
db.Set("gorm:save_associations", false).Save(&user)
```
### Extra Updating option ### Extra Updating option
```go ```go

4
documents/preload.md Normal file
View File

@ -0,0 +1,4 @@
# CRUD: Reading and Writing Data
Preloading (Eager loading)

View File

@ -18,6 +18,10 @@ You should always use paths and the `.md` extensions when linking to your files,
Yes, GitBooks can be created in [sub-directories](structure.md#subdirectory). GitBook.com and the CLI also looks by default in a serie of [folders](structure.md). Yes, GitBooks can be created in [sub-directories](structure.md#subdirectory). GitBook.com and the CLI also looks by default in a serie of [folders](structure.md).
#### Does GitBook supports RTL languages?
Yes, GitBook automatically detect the direction in your pages (`rtl` or `ltr`) and adjust the layout accordingly. The direction can also be specified globally in the [book.json](config.md).
--- ---
#### Does GitBook support Math equations? #### Does GitBook support Math equations?

View File

@ -0,0 +1,89 @@
# Context and APIs
GitBooks provides different APIs and contexts to plugins. These APIs can vary according to the GitBook version being used, your plugin should specify the `engines.gitbook` field in `package.json` accordingly.
#### Book instance
The `Book` class is the central point of GitBook, it centralize all access read methods. This class is defined in [book.js](https://github.com/GitbookIO/gitbook/blob/master/lib/book.js).
```js
// Read configuration from book.json
var value = book.config.get('title', 'Default Value');
// Resolve a filename to an absolute path
var filepath = book.resolve('README.md');
```
#### Output instance
The `Output` class represent the output/write process.
```js
// Return root folder for the output
var root = output.root();
// Resolve a file in the output folder
var filepath = output.resolve('myimage.png');
// Convert a filename to an URL (returns a path to an html file)
var fileurl = output.toURL('mychapter/README.md');
// Write a file in the output folder
output.write('hello.txt', 'Hello World')
.then(function() { ... });
// Copy a file to the output folder
output.copyFile('./myfile.jpg', 'cover.jpg')
.then(function() { ... });
// Verify that a file exists
output.hasFile('hello.txt')
.then(function(exists) { ... });
```
#### Page instance
A page instance represent the current parsed page.
```js
// Title of the page (from SUMMARY)
page.title
// Content of the page (Markdown/Asciidoc/HTML according to the stage)
page.content
// Relative path in the book
page.path
// Absolute path to the file
page.rawPath
// Type of parser used for this file
page.type ('markdown' or 'asciidoc')
```
#### Context for Blocks and Filters
Blocks and filters have access to the same context, this context is bind to the template engine execution:
```js
{
// Current templating syntax
"ctx": {
// For example, after a {% set message = "hello" %}
"message": "hello"
},
// Book instance
"book" <Book>,
// Output instance
"output": <Output>
}
```
For example a filter or block function can access the current book using: `this.book`.
#### Context for Hooks
Hooks only have access to the `<Book>` instance using `this.book`.

View File

@ -0,0 +1,62 @@
# Extend Blocks
Extending templating blocks is the best way to provide extra functionalities to authors.
The most common usage is to process the content within some tags at runtime. It's like [filters](./filters.md), but on steroids because you aren't confined to a single expression.
### Defining a new block
Blocks are defined by the plugin, blocks is a map of name associated with a block descriptor. The block descriptor needs to contain at least a `process` method.
```js
module.exports = {
blocks: {
tag1: {
process: function(block) {
return "Hello "+block.body+", How are you?";
}
}
}
};
```
The `process` should return the html content that will replace the tag. Refer to [Context and APIs](./api.md) to learn more about `this` and GitBook API.
### Handling block arguments
Arguments can be passed to blocks:
```
{% tag1 "argument 1", "argument 2", name="Test" %}
This is the body of the block.
{% endtag1 %}
```
And arguments are easily accessible in the `process` method:
```js
module.exports = {
blocks: {
tag1: {
process: function(block) {
// block.args equals ["argument 1", "argument 2"]
// block.kwargs equals { "name": "Test" }
}
}
}
};
```
### Handling sub-blocks
A defined block can be parsed into different sub-blocks, for example let's consider the source:
```
{% myTag %}
Main body
{% subblock1 %}
Body of sub-block 1
{% subblock 2 %}
Body of sub-block 1
{% endmyTag %}
```

View File

@ -8,13 +8,24 @@ A GitBook plugin is a node package published on NPM that follow a defined conven
The `package.json` is a manifest format for describing **Node.js modules**. GitBook plugins are built on top of Node modules. It declares dependencies, version, ownership, and other information required to run a plugin in GitBook. This document describes the schema in detail. The `package.json` is a manifest format for describing **Node.js modules**. GitBook plugins are built on top of Node modules. It declares dependencies, version, ownership, and other information required to run a plugin in GitBook. This document describes the schema in detail.
``` A plugin manifest `package.json` can also contain details about the required configuration. The configuration schema is defined in the `gitbook` field of the `package.json` (This field follow the [JSON-Schema](http://json-schema.org) guidelines):
```js
{ {
"name": "gitbook-plugin-mytest", "name": "gitbook-plugin-mytest",
"version": "0.0.1", "version": "0.0.1",
"description": "This is my first GitBook plugin", "description": "This is my first GitBook plugin",
"engines": { "engines": {
"gitbook": ">1.x.x" "gitbook": ">1.x.x"
},
"gitbook": {
"properties": {
"myConfigKey": {
"type": "string",
"default": "it's the default value",
"description": "It defines my awesome config!"
}
}
} }
} }
``` ```

View File

@ -0,0 +1,57 @@
# Extend Filters
Filters are essentially functions that can be applied to variables. They are called with a pipe operator (`|`) and can take arguments.
```
{{ foo | title }}
{{ foo | join(",") }}
{{ foo | replace("foo", "bar") | capitalize }}
```
### Defining a new filter
Plugins can extend filters by defining custom functions in their entry point under the `filters` scope.
A filter function takes as first argument the content to filter, and should return the new content.
Refer to [Context and APIs](./api.md) to learn more about `this` and GitBook API.
```js
module.exports = {
filters: {
hello: function(name) {
return 'Hello '+name;
}
}
};
```
The filter `hello` can then be used in the book:
```
{{ "Aaron"|hello }}, how are you?
```
### Handling block arguments
Arguments can be passed to filters:
```
Hello {{ "Samy"|fullName("Pesse", man=true}} }}
```
Arguments are passed to the function, named-arguments are passed as a last argument (object).
```js
module.exports = {
filters: {
fullName: function(firstName, lastName, kwargs) {
var name = firstName + ' ' + lastName;
if (kwargs.man) name = "Mr" + name;
else name = "Mrs" + name;
return name;
}
}
};
```

View File

@ -0,0 +1,90 @@
# Hooks
Hooks is a method of augmenting or altering the behavior of the process, with custom callbacks.
### List of hooks
### Relative to the global pipeline
| Name | Description | Arguments |
| ---- | ----------- | --------- |
| `init` | Called after parsing the book, before generating output and pages. | None |
| `finish:before` | Called after generating the pages, before copying assets, cover, ... | None |
| `finish` | Called after everything else. | None |
### Relative to the page pipeline
> It is recommended using [templating](./templating.md) to extend page parsing.
| Name | Description | Arguments |
| ---- | ----------- | --------- |
| `page:before` | Called before running the templating engine on the page | Page Object |
| `page` | Called before outputting and indexing the page. | Page Object |
##### Page Object
```js
{
// Parser named
"type": "markdown",
// File Path relative to book root
"path": "page.md",
// Absolute file path
"rawpath": "/usr/...",
// Title of the page in the SUMMARY
"title": "",
// Content of the page
// Markdown/Asciidoc in "page:before"
// HTML in "page"
"content": "# Hello"
}
```
##### Example to add a title
In the `page:before` hook, `page.content` is the markdown/asciidoc content.
```js
{
"page:before": function(page) {
page.content = "# Title\n" +page.content;
return page;
}
}
```
##### Example to replace some html
In the `page` hook, `page.content` is the HTML generated from the markdown/asciidoc conversion.
```js
{
"page": function(page) {
page.content = page.content.replace("<b>", "<strong>")
.replace("</b>", "</strong>");
return page;
}
}
```
### Asynchronous Operations
Hooks callbacks can be asynchronous and return promises.
Example:
```js
{
"init": function() {
return writeSomeFile()
.then(function() {
return writeAnotherFile();
});
}
}
```

View File

@ -2,7 +2,7 @@
Since version 3.0.0, GitBook can be easily themed. Books are using by default the [theme-default](https://github.com/GitbookIO/theme-default). Since version 3.0.0, GitBook can be easily themed. Books are using by default the [theme-default](https://github.com/GitbookIO/theme-default).
The theme to use is specified in the [book's configuration](config.md) using key `theme`. The theme to use is specified in the [book's configuration](../config.md) using key `theme`.
> **Caution**: Custom theming can block some plugins from working correctly. > **Caution**: Custom theming can block some plugins from working correctly.

View File

@ -70,9 +70,10 @@ function TemplateEngine(output) {
TemplateEngine.prototype.bindContext = function(func) { TemplateEngine.prototype.bindContext = function(func) {
var ctx = { var ctx = {
ctx: this.ctx, ctx: this.ctx,
output: this.output, book: this.book,
generator: this.output.name output: this.output
}; };
error.deprecateField(ctx, 'generator', this.output.name, '"generator" property is deprecated, use "output.generator" instead');
return _.bind(func, ctx); return _.bind(func, ctx);
}; };

View File

@ -68,7 +68,7 @@
data-chapter-title="Getting Started with GORM" data-chapter-title="Getting Started with GORM"
data-filepath="README.md" data-filepath="README.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -353,7 +353,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

View File

@ -70,7 +70,7 @@
data-chapter-title="Models" data-chapter-title="Models"
data-filepath="models.md" data-filepath="models.md"
data-basepath="." data-basepath="."
data-revision="Tue Mar 08 2016 12:18:21 GMT+0800 (CST)" data-revision="Tue Mar 08 2016 23:14:38 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -355,7 +355,7 @@
<li class="chapter " data-level="3.3" data-path="curd.html"> <li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading"> <a href="curd.html#preloading-eager-loading">
<b>3.3.</b> <b>3.3.</b>

4
preload.md Normal file
View File

@ -0,0 +1,4 @@
# CRUD: Reading and Writing Data
Preloading (Eager loading)

File diff suppressed because one or more lines are too long