Created
June 11, 2016 20:13
-
-
Save TechplexEngineer/684b7ffd142ce5170467e868138f32f3 to your computer and use it in GitHub Desktop.
PathFor template helper designed to imitate the functionality of the Iron:Router pathFor helper but using Flow Router in Meteor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Template.registerHelper('pathFor', function (options) { | |
| var routeName; | |
| if (arguments.length > 1) { | |
| routeName = arguments[0]; | |
| options = arguments[1] || {}; | |
| } | |
| var opts = options && options.hash; | |
| opts = opts || {}; | |
| var path = ''; | |
| var query = opts.query; | |
| var hash = opts.hash; | |
| var routeName = routeName || opts.route; | |
| var data = _.extend({}, opts.data || this); | |
| var keys = Object.keys(FlowRouter.current().route._params.keys); | |
| _.each(keys, function (keyConfig) { | |
| var key = keyConfig.name; | |
| if (_.has(opts, key)) { | |
| data[key] = EJSON.clone(opts[key]); | |
| // so the option doesn't end up on the element as an attribute | |
| delete opts[key]; | |
| } | |
| }); | |
| path = FlowRouter.path(routeName, data, query); | |
| return path; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment