Loading data
A route can connect to the server and name the method to call and the response populates the containers
before the view renders. Componyx ships backends for .NET and Node.js that handle the transport and the
merge rules, so with one of those in place there is no fetch to write on the client: by the time your
bindings are evaluated the data is already there.
$bindary.addRoute({
path: /^products\/(\d+)$/,
viewURL: '/views/product.html',
controllerURL: '/scripts/product.js',
dataConnect: true,
dataRequest: { id: '$1' },
dataServerLoad: 'Shop.Product.Controller.Load'
});
The response names the container it targets, so one call can fill viewData for this page and
top up appData with something the whole application needs. It also carries rules describing how
the incoming data is merged and later responses can arrive unprompted over the same connection to patch
what is already there. See routes and
backend for the server side.
Setting up data yourself
Without a server connection or for data that only exists in the browser, assign it yourself. That is also
the route to take if you would rather fetch it your own way: the containers are plain objects, so anything
that can produce one can fill them.
The route's controller is the natural place for it. A controller is a plain object of methods, named after
the events they handle.
(function (ctrl)
{
ctrl.load = () =>
{
$bindary.appData.demo = { intro: { count: 1 } };
};
ctrl.addCount = () => $bindary.appData.demo.intro.count++;
})($bindary.controller);
See controller for the full set of methods and their arguments.
Events
Every event can be handled in two places. The active route's controller handles it through a method of the
same name and any code can subscribe globally through the matching event object. Both run, controller
first.
ctrl.postRender = () => { }; // this route only
$bindary.onPostRender.add(function () { }); // every route
load and postLoad bracket a route: load after the route's data and
sources are in, postLoad once the view has rendered. preRender and
postRender bracket every render pass instead, so they run again on each update rather than only
at route load. Reach for postRender when you need the DOM for something that can change after
the first render and postLoad for one-time setup.
The data pipeline has its own pairs: preDataBind and postDataBind around input
binding, preDataUpdate and postDataUpdate around incoming server data. Returning
false from a pre-event cancels what follows, which is where validation and rejection belong.