Below is my router configuration on my spring mvc.
My question is how do I map parameter with router url, so when someone navigates to the url via pasting or refreshing, it will render the exact page.
Example url: http://localhost:8080/techtalks/#/viewMore/12345
publicRouter.js
define(['jquery', 'underscore', 'backbone',
'views/publicModule/viewMoreView',
], function($, _, Backbone,
ViewMoreView
) {
var AppRouter = Backbone.Router.extend({
routes: {
// Define some URL routes
'viewMore': 'viewMoreView',
// Default
'*actions': 'defaultAction'
}
});
var initialize = function() {
var app_router = new AppRouter;
app_router.on('route:viewMoreView', function() {
// Call render on the module we loaded in via the dependency array
ViewMoreView.render();
});
Backbone.history.start();
};
return {
initialize: initialize
};
});
Backbone view
define(['jquery', 'underscore', 'backbone',
'text!../../../viewMore.html'
], function($, _, Backbone, adminHomeTemplate) {
var ViewMoreView = Backbone.View.extend({
publicArticleTbl: null,
el: $("#publicPanel"),
render: function() {
var data = {};
publicArticleTbl = null;
// template
var compiledTemplateAdminHome = _.template(
adminHomeTemplate, data);
// append the item to the view's target
this.$el.html(compiledTemplateAdminHome);
},
// Event Handlers
events: {
},
});
return new ViewMoreView;
});
Use the callback directly, there's no need to use router events.
Also, catch the
idparam from the url.Then the view:
Backbone works with standard links, no need to have fancy things to call a route.
When making a module, generally, you return the constructor instead of an instance. This helps when reusing the same view type. Return an instance when the module is a global service.