├── .versions ├── package.js ├── LICENSE ├── iron_query.js └── README.md /.versions: -------------------------------------------------------------------------------- 1 | base64@1.0.3 2 | blaze@2.1.2 3 | cottz:iron-query@1.2.5 4 | deps@1.0.7 5 | ejson@1.0.6 6 | geojson-utils@1.0.3 7 | htmljs@1.0.4 8 | id-map@1.0.3 9 | iron:core@1.0.7 10 | iron:location@1.0.7 11 | iron:url@1.0.7 12 | jquery@1.11.3_2 13 | json@1.0.3 14 | meteor@1.1.6 15 | minimongo@1.0.8 16 | observe-sequence@1.0.6 17 | ordered-dict@1.0.3 18 | random@1.0.3 19 | reactive-var@1.0.5 20 | tracker@1.0.7 21 | ui@1.0.6 22 | underscore@1.0.3 23 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'cottz:iron-query', 3 | summary: 'simple package to add and take parameters in the route', 4 | version: '1.2.5', 5 | git: 'https://github.com/Goluis/cottz-iron-query' 6 | }); 7 | 8 | Package.onUse(function(api) { 9 | api.versionsFrom('1.1.0.2'); 10 | 11 | api.use('tracker'); 12 | api.use('ui'); 13 | api.use('iron:location@1.0.4'); 14 | api.use('iron:url@1.0.4'); 15 | 16 | api.addFiles('iron_query.js', 'client'); 17 | }); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /iron_query.js: -------------------------------------------------------------------------------- 1 | var path, pathname, queryObj, query = {}; 2 | 3 | query.keyDeps = {}; 4 | 5 | query.ensureKey = function (key) { 6 | var deps = this.keyDeps; 7 | if (!(key in deps)) 8 | deps[key] = new Tracker.Dependency; 9 | deps[key].depend(); 10 | }; 11 | 12 | query.get = function (key) { 13 | if (key) { 14 | this.ensureKey(key); 15 | return path.queryObject[key]; 16 | } else { 17 | if (!this.dep) 18 | this.dep = new Tracker.Dependency; 19 | 20 | this.dep.depend(); 21 | return path.queryObject; 22 | } 23 | }; 24 | 25 | query.set = function (key, val) { 26 | this.wait(key, val); 27 | this.go(); 28 | }; 29 | 30 | query.getNonreactive = function (key) { 31 | return key ? queryObj[key]: queryObj; 32 | }; 33 | 34 | query.wait = function (key, val) { 35 | var oldVal = queryObj[key]; 36 | if (String(val) === oldVal || !oldVal && !val) 37 | return; 38 | 39 | if (typeof val != 'undefined' && val != null && val != '') 40 | queryObj[key] = String(val); 41 | else 42 | delete queryObj[key]; 43 | 44 | var dep = this.keyDeps[key]; 45 | dep && dep.changed(); 46 | 47 | this.dep && this.dep.changed(); 48 | }; 49 | 50 | query.go = function () { 51 | var query = Iron.Url.toQueryString(queryObj); 52 | Iron.Location.go(pathname + query + path.hash); 53 | }; 54 | 55 | Iron.query = query; 56 | 57 | Tracker.autorun(function () { 58 | path = Iron.Location.get(); 59 | 60 | if (pathname) { 61 | var deps = query.keyDeps; 62 | for (var key in deps) { 63 | deps[key].changed(); 64 | }; 65 | query.dep && query.dep.changed(); 66 | } 67 | 68 | pathname = path.pathname; 69 | queryObj = path.queryObject; 70 | }); 71 | 72 | UI.registerHelper('ironQuery', function (key) { 73 | return query.getNonreactive(key); 74 | }); 75 | 76 | UI.registerHelper('ironQueryReactive', function (key) { 77 | return query.get(key); 78 | }); 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cottz-iron-query 2 | simple package to add and take parameters in the route with Meteor 3 | 4 | ## Installation 5 | ```sh 6 | $ meteor add cottz:iron-query 7 | ``` 8 | 9 | ## API 10 | `Iron.query` works exactly as a session but their values are stored in the route and depend of the route 11 | 12 | #### Iron.query.set (key, value) 13 | add or change the value of a key in the path 14 | 15 | #### Iron.query.get (key) 16 | gets the value of a key in the current path, if not sends `key` return an object with all the query keys 17 | 18 | #### Iron.query.getNonreactive (key) 19 | is exactly equal to `Iron.query.get` but non reactive (that description kills people) 20 | 21 | ## UI Helpers 22 | helpers available in the template 23 | 24 | #### {{ ironQuery key }} 25 | gets the value of a key in the current path, this helper uses Iron.query.getNonreactive because in most cases 26 | rerunning the helper in the template is not required 27 | 28 | #### {{ ironQueryReactive key }} 29 | is exactly equal to `ironQuery` but reactive (captain obvious help me) 30 | 31 | ## Other utilities 32 | 33 | #### Iron.query.wait(key, value) 34 | add or change the value of a key in the path but without redirect 35 | 36 | #### Iron.query.go() 37 | redirects to the path with the current keys object 38 | 39 | to further clarify the above `Iron.query.set` is this 40 | ````js 41 | Iron.query.set = function (key, value) { 42 | this.wait(key, value); 43 | this.go(); 44 | }; 45 | ```` 46 | 47 | ## Integration with iron router and fastRender 48 | [Iron Router](https://github.com/EventedMind/iron-router) - [FastRender](https://github.com/meteorhacks/fast-render) 49 | 50 | ````js 51 | Router.route('/home', { 52 | // with fastRender waiton is not very useful so I use subscriptions 53 | subscriptions: function () { 54 | var query = this.params.query; 55 | 56 | return [ 57 | Meteor.subscribe('news', { 58 | skip: Number(query.skip), 59 | search: query.q 60 | }); 61 | ]; 62 | }, 63 | fastRender: true 64 | }); 65 | 66 | /* somewhere in the client-side */ 67 | Iron.query.set('skip', 10); 68 | Iron.query.set('q', 'beautiful places'); 69 | ```` 70 | --------------------------------------------------------------------------------