Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加一个js端缓存扩展 kizzy #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions protected/controllers/front/Test1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class Test1Controller extends Controller
{

public function actionKizzy(){
$this->render('kizzy');
}

public function actionJqSimpleMenu(){
$this->render('menu/jqSimpleMenu');
Expand Down
3 changes: 2 additions & 1 deletion protected/modules/test/services/Test2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Test2Service implements ITestService
*/
public function helloTo($param = '')
{
return 'yes '.$param;
throw new CException('yyy');
// return 'yes '.$param;
}
}
126 changes: 126 additions & 0 deletions protected/my/widgets/kizzy/Kizzy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
*
* User: yiqing
* Date: 13-7-11
* Time: 下午8:59
* To change this template use File | Settings | File Templates.
* -------------------------------------------------------
* @see https://github.com/ded/Kizzy
* @see http://www.dustindiaz.com/javascript-cache-provider
* -------------------------------------------------------
*/

class Kizzy extends CWidget
{

/**
* @var string
*/
public $assetsPath ;

/**
* @var string
*/
public $baseUrl;

/**
* @var bool
*/
public $debug = YII_DEBUG;

/**
* @var \CClientScript
*/
protected $cs;


public $jsFilePos = CClientScript::POS_HEAD;

/**
* @return $this
*/
public function publishAssets()
{
if (empty($this->baseUrl)) {
$this->assetsPath = $assetsPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
if ($this->debug == true) {
$this->baseUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, true);
} else {
$this->baseUrl = Yii::app()->assetManager->publish($assetsPath);
}
}
return $this;
}


/**
*
*/
public function init()
{

parent::init();
$this->cs = Yii::app()->getClientScript();
// publish assets and register css/js files
$this->publishAssets();
if ($this->debug) {
$this->registerScriptFile('kizzy.js', $this->jsFilePos);
} else {
// no mini file now !
$this->registerScriptFile('kizzy.min.js', $this->jsFilePos);
}

}

/**
* @param $fileName
* @param $position
* @return $this
* @throws InvalidArgumentException
*/
protected function registerScriptFile($fileName, $position = CClientScript::POS_END)
{
if (is_string($fileName)) {
$jsFiles = explode(',', $fileName);
} elseif (is_array($fileName)) {
$jsFiles = $fileName;
} else {
throw new InvalidArgumentException('you must give a string or array as first argument , but now you give' . var_export($fileName, true));
}
foreach ($jsFiles as $jsFile) {
$jsFile = trim($jsFile);
$this->cs->registerScriptFile($this->baseUrl . '/' . ltrim($jsFile, '/'), $position);
}
return $this;
}


/**
* @param $fileName
* @return $this
* @throws InvalidArgumentException
*/
protected function registerCssFile($fileName)
{
$cssFiles = func_get_args();
foreach ($cssFiles as $cssFile) {
if (is_string($cssFile)) {
$cssFiles2 = explode(',', $cssFile);
} elseif (is_array($cssFile)) {
$cssFiles2 = $cssFile;
} else {
throw new InvalidArgumentException('you must give a string or array as first argument , but now you give' . var_export($cssFiles, true));
}
foreach ($cssFiles2 as $css) {
$this->cs->registerCssFile($this->baseUrl . '/' . ltrim($css, '/'));
}
}
// $this->cs->registerCssFile($this->assetsUrl . '/vendors/' .$fileName);
return $this;
}


//---------------------------------------------------------

}
2 changes: 2 additions & 0 deletions protected/my/widgets/kizzy/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
18 changes: 18 additions & 0 deletions protected/my/widgets/kizzy/assets/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[submodule "build/UglifyJS"]
path = build/UglifyJS
url = https://github.com/mishoo/UglifyJS.git
[submodule "build/jshint"]
path = build/jshint
url = https://github.com/jshint/jshint
[submodule "build/gzip"]
path = build/gzip
url = https://github.com/indutny/node.gzip.git
[submodule "build/scriptjs"]
path = build/scriptjs
url = https://github.com/ded/script.js.git
[submodule "build/json"]
path = build/json
url = https://github.com/douglascrockford/JSON-js.git
[submodule "build/sink"]
path = build/sink
url = git://github.com/ded/sink-test.git
2 changes: 2 additions & 0 deletions protected/my/widgets/kizzy/assets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
boosh:
smoosh make make/build.js
90 changes: 90 additions & 0 deletions protected/my/widgets/kizzy/assets/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Kizzy - a Local Storage Utility

Kizzy is a light-weight, cross-browser, JavaScript local storage utility. It leverages the HTML5 localStorage API when available, as well as Internet Explorer's persistent XML store — wrapped up in a easy to use, memcached-like interface. When neither of these features are available (unlikely), it falls back to an in-browser object store.

It looks like this

``` js
var cache = kizzy('users')
var agent = cache.get('Agent')
if (agent) {
alert('Welcome back ' + agent.name)
} else {
cache.set('Agent', {
name: 'Agent Diaz'
})
}
```

Furthermore, a call to 'set' will return the value, making it quite easy for assignment.

``` js
var cache = kizzy('users')
var agent = cache.get('Agent') || cache.set('Agent', {
name: 'Agent Diaz'
})
```

Lastly, you can pass an optional third argument to 'set' that tells the cache how long to live

``` js
var cache = kizzy('users')

var agent = cache.get('Agent') || cache.set('Agent', {
name: 'Agent Diaz'
}, 5000) // time to live set for 5 seconds


// wait 3 seconds...
setTimeout(function() {
alert('Still there ' + cache.get('Agent').name)
}, 3000)

// 6 seconds later...
setTimeout(function() {
cache.get('Agent').name // => expired
}, 6000)
```

# Browser support

* Internet Explorer 6+
* Firefox 2+ (when localStorage is enabled (the browser default))
* Chrome
* Safari 4+
* Opera

# Building Kizzy

``` sh
$ submodule update --init
& make
```

# Running tests

Tests will not currently pass if run on a file:/// protocol. Otherwise...

``` sh
$ open tests/test.html
```

# Ender integration

Install Kizzy as an Ender module

``` sh
$ ender add kizzy
```

Use it as such:

``` js
$.cache('user').get('name')
```

# Kizzy whu?

The name comes from Kunta Kinte, a Mandinka African warrior from the 1700's. After being brought into slavery, he had a daughter whom he named Kizzy, which translates to *stay put* in hopes that the family would stay together, but not stay a slave.

Happy Caching!
Loading