Skip to content

Commit

Permalink
Migrate Cookie docs and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Jul 25, 2011
1 parent c1a3fd2 commit 936ee2c
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/cookie/docs/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name" : "cookie",
"displayName": "Cookie Utility",
"description": "Provides a simple API for interacting with cookies, including the creation and manipulation of subcookies.",
"author" : "nzakas",

"tags": ["utility", "cookie", "cookies"],
"use" : ["cookie"],

"examples": [
{
"name" : "cookie-simple-example",
"displayName": "Simple Cookie Example",
"description": "Demonstrates basic usage of the Cookie utility for reading and writing cookies.",
"modules" : ["cookie"],
"tags" : ["cookie"],

"hideTableOfContents": true
},

{
"name" : "cookie-advanced-example",
"displayName": "Advanced Cookie Example",
"description": "Demonstrates using the Cookie utility to get, set and remove cookies.",
"modules" : ["cookie"],
"tags" : ["cookie"],

"hideTableOfContents": true
},

{
"name" : "cookie-subcookie-example",
"displayName": "Subcookie Example",
"description": "Demonstrates using the Cookie utility to get and set subcookies.",
"modules" : ["cookie"],
"tags" : ["cookie"],

"hideTableOfContents": true
}
]
}
48 changes: 48 additions & 0 deletions src/cookie/docs/cookie-advanced-example.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div class="intro">
<p>This example shows how to get, set, and remove cookies using the YUI Cookie utility.</p>
</div>

<div class="example yui3-skin-sam">
{{>cookie-advanced-example-source}}
</div>

<h2>Description</h2>

<p>This example consists of three buttons, each of which performs one of the basic cookie functions: getting a value, setting a value, and removing a value. The first button, &quot;Get Value&quot;, retrieves the value of a cookie and displays it in an alert:</p>

```
Y.one("#yui-cookie-btn1").on("click", function () {
var value = Y.Cookie.get("example");
alert(value);
Y.log("Cookie 'example' has a value of '" + value + "'");
});
```

<p>The second button, &quot;Set Random Value&quot;, creates a random value and sets the cookie's value equal to it:</p>

```
Y.one("#yui-cookie-btn2").on("click", function () {
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000);
Y.Cookie.set("example", newValue);
Y.log("Set cookie 'example' to '" + newValue + "'");
});
```

<p>After clicking this button, you can go back and click &quot;Get Value&quot; to see the new value that was assigned
to the cookie (you can also check the logger output).</p>
<p>The third button, &quot;Remove Value&quot;, completely removes the cookie from the page:</p>

```
Y.one("#yui-cookie-btn3").on("click", function () {
Y.Cookie.remove("example");
Y.log("Removed cookie 'example'.");
});
```

<p>When this button is clicked, it removes the cookie. If &quot;Get Value&quot; is clicked immediately afterwards, the value should be <code>null</code>.</p>

<h2>Complete Example Source</h2>

```
{{>cookie-advanced-example-source}}
```
33 changes: 33 additions & 0 deletions src/cookie/docs/cookie-simple-example.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="intro">
<p>This example shows basic usage of the YUI Cookie utility. The example checks the value of a cookie and then sets it to a new value.</p>
</div>

<div class="example yui3-skin-sam">
{{>cookie-simple-example-source}}
</div>

<h2>Description</h2>

<p>This example begins by getting the value of a cookie named &quot;example&quot;. If this is the first time you've run
the example, the value should be <code>null</code>:

```
var currentValue = Y.Cookie.get("example");
```

<p>This value is shown in the browser console. Next, the cookie is set to a random value:</p>

```
var newValue = "yui" + Math.round(Math.random() * Math.PI);
Y.Cookie.set("example", newValue);
```

<p>When you reload the page, the value of the cookie should be the one that was just set.</p>

<p>Note: this example uses session cookies, so the value will be lost when you close the browser.</p>

<h2>Complete Example Source</h2>

```
{{>cookie-simple-example-source}}
```
45 changes: 45 additions & 0 deletions src/cookie/docs/cookie-subcookie-example.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<div class="intro">
<p>This example shows how to get and set subcookies as well as using conversion functions when retrieving subcookie values.</p>
</div>

<div class="example yui3-skin-sam">
{{>cookie-subcookie-example-source}}
</div>

<h2>Description</h2>

<p>The first three lines attempt to read the values stored in subcookies of the &quot;example&quot; cookie:</p>

```
var name = Y.Cookie.getSub("example", "name");
var today = Y.Cookie.getSub("example", "today", function (value) {
return new Date(value);
});
var count = Y.Cookie.getSub("example", "count", Number);
```

<p>The &quot;name&quot; subcookie stores a string so it is retrieved without specifying a third argument.</p>

<p>The &quot;today&quot; subcookie stores a date string, which should be converted to a <code>Date</code> object upon retrieval; the third argument of <code>getSub()</code> is specified as a custom function that will convert the returned value into a <code>Date</code> object.</p>

<p>The &quot;count&quot; subcookie contains a number and is converted to an actual JavaScript number by passing in the native <code>Number</code> function.</p>

<p>If any of these subcookies don't exist, <code>getSub()</code> returns <code>null</code>. This should be the case the first time you run the example.</p>

<p>The retrieved values are shown in the browser console.</p>

<p>After that, new values are assigned to the various subcookies:</p>

```
Y.Cookie.setSub("example", "name", "Yahoo!");
Y.Cookie.setSub("example", "today", (new Date()).toString());
Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30));
```

<p>The &quot;name&quot; subcookie is set to &quot;Yahoo!&quot;, the &quot;today&quot; subcookie is set to the value of a new <code>Date</code> object as a string, and the &quot;count&quot; subcookie is filled with a random number. The next time you run the example, the subcookies should have these values.</p>

<h2>Complete Example Source</h2>

```
{{>cookie-subcookie-example-source}}
```
Loading

0 comments on commit 936ee2c

Please sign in to comment.