问题描述:
A single page JavaScript application has multiple components representing the same data model in different ways. Some of these views are read only and simply show the data in the model, while some allow the model to be mutated. A simple data model might look like this:
The initial model is retrieved by a view component using an API call such as:
let model = api.data.get(“LV-F9-1234”);
Assume that multiple view components are sharing the same model, but not the same object instance.
Assuming that we cannot use a framework such as Vuex or Redux, please extend this API with two new functions:
A function to mutate the given model, and inform all other views holding that model that their model is stale
A function that allows views to be altered when their model is stale. Think about how views will receive the latest version of the model, bearing in mind that views are not sharing the same model object instance
由于平时用react和redux,现在他让我用纯js,瞬间懵,大概就是界面A更新了数据,其他界面同步更新,类似订阅
然后他又给我两个function的提纲如下:
// Read-only view
function view(modelId) {
let model = api.data.get(modelId);
renderToDom(model);
//Subscribe for updates?
//api.data.?
//renderToDom(updatedModel);
}
/// Form view
function onFormChange(formModel) {
api.data.
}
可是这题目不是写说 please extend this API with two new functions
面完之后我自己写了如下两个,他给的两个function是不是实现的具体例子呢?有点晕
//publish/update
api.data.pub = (newModel, component) => {
let event = new Event(newModel.Id, newModel);
component.dispatchEvent(event);
}
//subscription
api.data.sub = (id, component) => {
component.addEventListener(id, (updateModel) => {
renderToDom(updatedModel)
})
}