Skip to content

Commit

Permalink
added ability to control the caching of template url, allowing templa…
Browse files Browse the repository at this point in the history
…te url set to server side pages
  • Loading branch information
muzammilkm committed Jan 19, 2019
1 parent 92dee3d commit 8b9365d
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 14 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Built on jQuery 1.3.0 & Inspried by (Angular ui-router & jQuery Single Page Appl
## Demo
* [Simple](https://muzammilkm.github.io/jq-router/docs/simple/index.html)
* Easy & Simple
* [Simple-Non-Cache](https://muzammilkm.github.io/jq-router/docs/simple-non-cache/index.html)
* Easy & Simple
* Cachable Routes
* [Nested Views](https://muzammilkm.github.io/jq-router/docs/nested-views/index.html)
* Nested View (Parent > Child > Grand Child)
* [Lazy Store](https://muzammilkm.github.io/jq-router/docs/lazy-store/index.html)
Expand Down Expand Up @@ -97,6 +100,7 @@ Routes is collection of route objects. Each route object consists of url, templa

route["parent.child.grandchild"] = {
url: '',
cache: bool,
templateUrl: ''
}
```
Expand All @@ -113,13 +117,30 @@ url: ''
```javascript
templateUrl: ''
```
> Allowing caching of route template or you can also set default to all routes in setData,
```javascript
cahce: true || false
```
> Path to render the view in matched view selector.
```javascript
resolve: callback //function or [] of function
```
> A callback function or array of function's which is executed when a route is matched & route is rendered only when all deferred objects are resolved.s
```javascript
$.router.setData(data, isCacheTempalte);
```
> setDate takes two parameters
* data: A route object which contains route definition, like url, template url, route is parent & should it be cached.
* isCacheTempalte: If the templates are server side pages (like php, aspx, jsp, or server rendered). Then you set this to false & templates are not cached.

```javascript
$.router.setDefault(name);
```
> setDefault
* name: A route name, if the url does not match to any route, router will redirect to default secified route.

```javascript
$.when($.ready)
.then(function() {
Expand Down
11 changes: 7 additions & 4 deletions dist/jq-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@
* @param {object} data
* @return {object} this
*/
s.setData = function(data) {
s.setData = function(data, isCacheTempalte) {
var s = this;

isCacheTempalte = isCacheTempalte === undefined ? true : isCacheTempalte;
s.routes = {};
for (var routeName in data) {
var segments = routeName.split('.'),
Expand All @@ -226,6 +227,7 @@
urlExpr = new RegExp("^" + relativeUrl.replace(expr.Param_Matcher, expr.Param_Replacer) + "$");
route.relativeUrl = relativeUrl;
route.urlExpr = urlExpr;
route.cache = route.hasOwnProperty("cache") ? route.cache : isCacheTempalte;
}
return s;
};
Expand Down Expand Up @@ -403,9 +405,10 @@
* @param {string} url
* @return {object} deferred
*/
s.getViewTemplate = function(url) {
s.getViewTemplate = function(url, cache) {
return $.get({
url: url,
cache: cache,
dataType: 'html'
})
.then(function(content) {
Expand Down Expand Up @@ -435,8 +438,8 @@
requests.push(_route.resolve(route, params));
}

if (!templateCache[_route.templateUrl]) {
requests.push(s.getViewTemplate(_route.templateUrl));
if (!templateCache[_route.templateUrl] || !_route.cache) {
requests.push(s.getViewTemplate(_route.templateUrl, _route.cache));
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/jq-router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
<li>Easy &amp; Simple</li>
</ul>
</li>
<li><a href="simple-non-cache/index.html">Simple Non Cacheable</a>
<ul>
<li>Easy &amp; Simple</li>
<li>Non Cacheable template</li>
<li>Non Cacheable template per route</li>
</ul>
</li>
<li><a href="nested-views/index.html">Nested Views</a>
<ul>
<li>Nested View (Parent &gt; Child &gt; Grand Child)</li>
Expand Down
11 changes: 7 additions & 4 deletions docs/scripts/jq-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@
* @param {object} data
* @return {object} this
*/
s.setData = function(data) {
s.setData = function(data, isCacheTempalte) {
var s = this;

isCacheTempalte = isCacheTempalte === undefined ? true : isCacheTempalte;
s.routes = {};
for (var routeName in data) {
var segments = routeName.split('.'),
Expand All @@ -226,6 +227,7 @@
urlExpr = new RegExp("^" + relativeUrl.replace(expr.Param_Matcher, expr.Param_Replacer) + "$");
route.relativeUrl = relativeUrl;
route.urlExpr = urlExpr;
route.cache = route.hasOwnProperty("cache") ? route.cache : isCacheTempalte;
}
return s;
};
Expand Down Expand Up @@ -403,9 +405,10 @@
* @param {string} url
* @return {object} deferred
*/
s.getViewTemplate = function(url) {
s.getViewTemplate = function(url, cache) {
return $.get({
url: url,
cache: cache,
dataType: 'html'
})
.then(function(content) {
Expand Down Expand Up @@ -435,8 +438,8 @@
requests.push(_route.resolve(route, params));
}

if (!templateCache[_route.templateUrl]) {
requests.push(s.getViewTemplate(_route.templateUrl));
if (!templateCache[_route.templateUrl] || !_route.cache) {
requests.push(s.getViewTemplate(_route.templateUrl, _route.cache));
}
}

Expand Down
49 changes: 49 additions & 0 deletions docs/simple-non-cache/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<html>
<title>JS Router | Basic</title>

<head></head>

<body>
<div class="menu">
<a href="#/">Home</a> | <a href="#/cache">Cached</a> | <a href="#/non-cache">Not Cached</a>
</div>
<div class="my-view"></div>
<footer class="footer" style="clear:both; text-align: center">
<p>jq-router is maintained by <a href="https://github.com/muzammilkm">muzammilkm</a></p>
<p>go to jq-router <a href="/jq-router">documentation</a></p>
</footer>
<script type="text/javascript" src="../scripts/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../scripts/jq-router.js"></script>
<script type="text/javascript">
var routes = {},
defaultRoute = 'home';

routes['home'] = {
url: '#/',
templateUrl: 'templates/home.html'
};

routes['cache-route'] = {
url: '#/cache',
cache: true,
templateUrl: 'templates/cache-route.html'
};

routes['non-cache-route'] = {
url: '#/non-cache',
cache: false,
templateUrl: 'templates/non-cache-route.html'
};

$.router
.setData(routes, false)
.setDefault(defaultRoute);

$.when($.ready)
.then(function() {
$.router.run('.my-view', 'home');
});
</script>
</body>

</html>
4 changes: 4 additions & 0 deletions docs/simple-non-cache/templates/cache-route.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Cache Page</h1>
<p>
Look in network tab...
</p>
1 change: 1 addition & 0 deletions docs/simple-non-cache/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Home</h1>
4 changes: 4 additions & 0 deletions docs/simple-non-cache/templates/non-cache-route.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Non-Cache Page</h1>
<p>
Look in network tab...
</p>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jq-router",
"version": "4.3.0",
"version": "4.5.0",
"description": "A tiny jQuery plugin for building single page application (SPA) with the possibility of nested views",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/jq-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@
/**
* Set route data by preparing params & expression.
* @param {object} data
* @param {bool} isCacheTempalte default to true
* @return {object} this
*/
s.setData = function(data) {
s.setData = function(data, isCacheTempalte) {
var s = this;

isCacheTempalte = isCacheTempalte === undefined ? true : isCacheTempalte;
s.routes = {};
for (var routeName in data) {
var segments = routeName.split('.'),
Expand All @@ -226,6 +228,7 @@
urlExpr = new RegExp("^" + relativeUrl.replace(expr.Param_Matcher, expr.Param_Replacer) + "$");
route.relativeUrl = relativeUrl;
route.urlExpr = urlExpr;
route.cache = route.hasOwnProperty("cache") ? route.cache : isCacheTempalte;
}
return s;
};
Expand Down
8 changes: 5 additions & 3 deletions src/services/jq-router-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
/**
* Download the template from server via ajax call.
* @param {string} url
* @param {bool} cache
* @return {object} deferred
*/
s.getViewTemplate = function(url) {
s.getViewTemplate = function(url, cache) {
return $.get({
url: url,
cache: cache,
dataType: 'html'
})
.then(function(content) {
Expand Down Expand Up @@ -67,8 +69,8 @@
requests.push(_route.resolve(route, params));
}

if (!templateCache[_route.templateUrl]) {
requests.push(s.getViewTemplate(_route.templateUrl));
if (!templateCache[_route.templateUrl] || !_route.cache) {
requests.push(s.getViewTemplate(_route.templateUrl, _route.cache));
}
}

Expand Down

0 comments on commit 8b9365d

Please sign in to comment.