Backbone routing change content with jquery
-
18-06-2021 - |
Question
Im using meteor so it has its own templating method based on handlebars
In most of the examples ive seen around of Backbone's method of routing they use alerts or something but im a bit confused on how to actually change the page's content
I use:
var fragment = Meteor.ui.render( function () {
return Template["page"]();
});
$('#content').html(fragment);
I also have a
<template name="page">...</template>
This of course does work, but It wont work if I load the page up with url#page
it wont do anything because $('#content')
is being loaded before the DOM is loaded up. But it would work ordinarily with hyperlinks on the page to url#page
.
How would I solve it so that the page would load up when loading it as a url without breaking it's use in hyperlinks. Moreover is my paradigm a right use?
Solution
Add a loadPage
helper to the content template, then, in that template helper put the Backbone routing code inside a Meteor.defer(function() { ... });
, this will make your routing code run after the content template has been added to the DOM, and thus will become accessible to jQuery.