查看: 1089| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 纯JS API问题,订阅同步更新--大神帮看看

全局:
高频题
公司名称: linkedin

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x

问题描述:
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:

{
Id: “LV-F9-1234”,
name: “Falcon 9”,
launchDate: “2019-03-07”,
launchSite: “Vandenberg AFB”
payloads: [{
  Id: “CRS-10”
  name: “Commercial Resupply Services Mission 10”
}]
}

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)   
    })
}

评分

参与人数 1大米 +15 收起 理由
14417335 + 15

查看全部评分


上一篇:刷题小白请问需要注意run time和memory use吗
下一篇:【干货】FLAG面了200+的面试官告诉你算法面试不是只有刷题这么简单
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表