javascript - JS ES6 构造一个具有类似于 Laravel 集合的原始和属性的对象
问题描述
我正在尝试创建一个可扩展的基模型类,它允许在 和 之间进行区分original
,attributes
以便我可以getDirty()
在子类上创建一个方法,该方法将在对象orignal
属性和任何更改的attributes
. 这是到目前为止的课程:
import Vue from 'vue'
class Model {
constructor (attributes) {
this.original = attributes
this.attributes = attributes
Object.keys(this.attributes).forEach((property) => {
Object.defineProperty(this, property, {
get: () => this.getAttribute(property),
set: (value) => this.setAttribute(property, value)
})
})
}
getAttribute (key) {
return this.attributes[key]
}
setAttribute (key, value) {
this.attributes[key] = value
}
static fetchById (id) {
if (!id) {
throw new Error('Cannot fetch without id')
}
return Vue.prototype.$http
.get(this.endpoint + id)
.then(response => {
return new this(response.data)
})
.catch(error => {
console.error(error)
})
}
}
export default Model
使用此代码,我通过使用 Account 模型扩展此模型来创建一个帐户:
import Model from './Model'
import Vue from 'vue'
class Account extends Model {
static endpoint = '/api/accounts/'
getOwner () {
if (this.company_owner_uuid) {
return 'company'
}
return 'person'
}
getDirty = () => {
// This log is showing the changed name from the component input, rather than the original
console.log(this.original.person_owner.first_name)
const diff = Object.keys(this.original).reduce((diff, key) => {
if (this.attributes[key] === this.original[key]) return diff
return {
...diff,
[key]: this.original[key]
}
}, {})
return diff
}
update () {
return Vue.prototype.$http
.put(`${Account.endpoint}/${this.account_uuid}`, this.getDirty())
.then(response => {
console.log(response)
return response.data
})
.catch(error => {
console.error(error)
})
}
}
export default Account
在我的组件中:
Account.fetchById(this.id).then(account => {
this.account = account
})
.catch(error => {
console.error(error)
})
.then(() => {
// hide loading or whatever after promise is fully resolved...
})
除了我对帐户进行更改之外,这一切都有效,它同时改变了this.original
和this.attributes
。是否有人建议创建this.original
帐户对象的不可变版本,该版本传递给构造函数,然后可用于与 mutated 进行比较attributes
?我的最终目标是只将任何修改后的属性发送到后端(我使用的是 Laravel 5.8)。谢谢!
解决方案
推荐阅读
- html - 如何创建一个向下跳跃的滚动,以增加视口的大小
- django - 如何使用 Apache(而不是 NGINX)将 Django + Postgres 应用程序 Dockerize 用于生产
- sql - 用于从 SQL CREATE TABLE 语句中提取表信息的正则表达式
- node.js - App Engine 上缺少全局变量值 Node.js
- javascript - 对数组中的每个值使用函数
- ruby-on-rails - 使用 cancancan 时确认设计用户时出错
- python - 根据十六进制值创建特定尺寸的图像
- c# - 从 CSV 文件中读取并将其内容“保存”到 List 对象并将它们加载到 Program.cs
- javascript - Duktape - 评估时 for of 循环出错
- django - 基于 django 类的 CreateApiView author_id 不为空?