Because I couldn't get standalone code from learn.knockoutjs.com I
created this repo to get started:
https://github.com/dscape/knockout.js.samples
I'm going to fix number 3 so it actually does the routes, e.g. if you
hit refresh you loose the current message and tab.
Then I'll try sugarskull.
If you manage to get sugarskull before I get to it please do let me know :)
Nuno
Nuno
So now we are ripping out backbones... Are we programmers, surgeons, or aliens?
In seriousness this stuff looks awesome. Can't wait to try it when I get home.
Do you have a sample demo you can show? Or maybe this is part of one
of your open source projects?
Would be really interesting to see a working example
Nuno
sugarskull is used exclusively for routing, nothing else
ko is used for everything else, but no crazy bindings
check: https://github.com/dscape/knockout.js.samples/tree/master/03-single-page-apps
now I might go and work on actually making page refresh work
nuno
https://github.com/dscape/knockout.js.samples/commit/9b64dc6806ecff459e5753edc68e00086b22326f
I add to add a method to sugarskull to pull it off but I'll submit a
pull request there so no one gets puzzled why it doesnt work in your
computer (check the differ to figure out where)
Nuno
Honestly, this whole routing thing has me very lost. I loved the results of the knockout address plugin, but I never did fully understand what was going on with it. RPN has explained a little bit.
I am a novice at best, and know nothing of these other frameworks, but it looks to me that avoiding more magic strings would yield a more modular design that can be maintained easier. One of the things I hate about javascript is the amount of string concatenation that is often required. (I guess this is very hypocritical of me since knockout uses string literals for binding, and I am a huge preponent of the asp.net mvc html helpers, which are really just string extensions that accept parameters... But I digress.)
It looks like the declarative routing would become obtuse very easily. Is there a particular reason you want to use this pattern?
I see your point. That does sounds like something you would want but
let's look at the downside of the decoupling:
- Routers behavior is driven by KO, instead of being declarative like
the one I wrote
- Complex logic in the routing system will be become harder to manage
the more variables and binds you have
- That would be a decision bound on (As you said) a single point of
truth. That point is not the router, so people user to developing apps
that center about routes would find that unsatisfying
- It mostly forces you to write bad and cryptic routing rules.
- Page refreshes will include code that is far more complicated than
the one I wrote. Plus the possibility of bugs in that code will
probably be much higher given the amount of variables to control. In
my case it goes route to vars, and works fine I think?
I'm sure some of this can be leveraged, and I'm really happy that KO
was flexible enough to allow me that solution of only keeping one
variable (the route) that I need to observe. That fits me quite nicely
and I think I can still leverage the fact that I can now introduce
bugs by being out of sync. However I feel like my aproach is actually
more decoupled, allowing you to leverage whatever routing system you
like with KO. That's really good, at least for someone like me who
enjoys minimalism and having small pieces of code doing small parts.
If you think about my approach you will also see that I'm basically
created a js href kind-of-thing which it won't be foreign to a web
developer at all.
Thanks you all for your input, ideas, and so on. Hope to have
contributed something back with examples, working code and some
thoughts :)
Nuno
, folder = v.selectedFolderko.link_observable_to_ss_route = function (v) {var routes = { '/:folder' : { on: update_bindings }, '/:folder/:email' : { on: update_bindings }}, router = Router(routes)
, email = v.selectedMailId, go = v.go;function update_bindings() {var current_route = router.currentRoute();if(folder && folder() !== current_route[0]) { folder(current_route[0]); }if(email && email() !== current_route[1]) { email(current_route[1]); }}
router.init("#/");
return go.subscribe( function (value) { return router.setRoute(value); });};
This just says they are going to use the observable plugin and punches plugin by default, they are not getting rid of knockout, I thought?On Wed, Apr 2, 2014 at 3:10 PM, John Farrar <johnf...@sosapps.com> wrote:NOPE... they are ditching Knockout for the next gen version of Druandal.
On Wednesday, April 2, 2014 12:55:37 PM UTC-4, gaffe wrote:
Why reinvent the wheel and roll your own when you can use something like this where it has been done for you:
--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/mnrStEiFz1o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
// set up the amd modules and templates
ko.bindingHandlers.module.baseDir = "modules";
ko.amdTemplateEngine.defaultPath = "modules";
ko.amdTemplateEngine.defaultSuffix = ".html";
$(function () {
// fire up Sammy
Sammy('#content', function () {
var route = function(method,path,content) {
this[method](path, function () {
var result;
if (_.isString(content)) {
result = {
name: content,
data: this.params
};
} else if (_.isPlainObject(content)) {
// get the properties of the sammy url parameters, jquery won't do a deep merge otherwise
var params = _.pick(this.params, _.keys(this.params));
// do a deep merge on the content and params so the child 'data' properties merge together
result = $.extend(true, {}, content, { data: params });
}
context.content(result);
});
}.bind(this);
// set up specific routes
route('get', '#/foo/:id/edit', { name: "foo/info/module", template: "foo/info/edit", data : { edit : true } }); // foo edit info page
route('get', '#/foo/:id', { name: "foo/info/module", template: "foo/info/view" }); // foo view info page
route('get', '#/foo', "foo/search/module"); // foo search page
route('get', '#/bar/:id', "bar/module"); // bar search page
route('get', '#/bar', "bar/module"); // bar search page
// generic routes
// route for module with id
this.get("#/:module/:id", function () {
context.content({
name: this.params.module + "/module",
data : this.params
});
});
// route for list module
this.get("#/:module", function () {
context.content(this.params.module + "/module");
});
}).run();
// only apply bindings after everything's been loaded
ko.applyBindings(context);
});