Answer the question
In order to leave comments, you need to log in
json instead of html
There is a user page, i.e. user wall (as on VKontakte or Facebook). With the help of "infinite scrolling" different types of content are loaded, i.e. comment, wall post, photo list, etc. The easiest way is to return html, but I would still like to work with json, you yourself understand why.
News, photo list, normal posts are the components, and the wall is the "user activity stream". In php, components communicate with each other using an internal API. But how to teach client-side components to communicate is still unclear.
Please help with advice on how best to design such functionality on the client side so that json can be used for rendering instead of html, taking into account that the components should exist autonomously, i.e. there should not be any hardcode in the "wall" component. Those. in the "wall" component should not be something like this:
if(component == 'news'){
//отобразить новость
} elseif(component == 'comment'){
//отобразить комментарий
}
Answer the question
In order to leave comments, you need to log in
There are various kinds of javascript template engines, I think you should look in this direction. For example handlebarsjs.com/
You can load the necessary components, like this scheme:
var callback=function() {
known_components[component_id](data);
}
if (!known_components[component_id]) {
load_component_js_from_server(component_id, callback)
} else {
callback();
}
Events to say.
In transport, on getting json something like this:
app.trigger('load'+data.component,data)
In components for this event:app.on('loadNews',function(data){/*парсим шаблон*/})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question