- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个情况,我有一个大而复杂的对象,我需要通过轻微修改(扩展属性等)在其他地方重新使用它
为了提高简单性和可重用性,我想我会尝试“扩展”原始对象。所以它看起来像这样......
(function ($, window) {
website.module('admin.models', function (exports, require) {
exports.item = {
// large, complex object that will be instantiated by other things
};
});
})(jQuery, window);
正在使用的库是 Telerik 的 Kendo UI
,我正在使用他们的 MVVM
系统。 item
函数实际上是被赋予模板的 kendo.data.Model.define
实例。 <强> documentation for Kendo.data.Model.define
所以这个用法就像...
var item = new website.admin.models.item({
// specific property values
});
这将创建一个可以绑定(bind)到页面的新 View 模型。底层模型有很多行为,因此更改 View 模型本身不会有覆盖或更改底层核心的风险。
这很好用。它将我需要的功能移动到一个单独的文件中,并清理了我更大的实现。这也是该函数的预期用途,由我正在使用的库的文档概述。
但是还有另一种模型称为schema
,它与item
基本相同,只是它有一些额外的属性。
与其复制并粘贴项目模型,我认为尝试扩展它更明智。所以我尝试了这种方法......
(function ($, window) {
website.module('admin.models', function (exports, require) {
exports.schema = $.extend({
Item: {
Id: null, // will be filled in on the UI
Name: null, // will be filled in on the UI
Description: null, // will be filled in on the UI
Timestamp: null // will be filled in on the UI
},
Editing: false
}, website.admin.models.item);
});
})(jQuery, window);
但这不起作用,因为给定的路径不是“item”的实例。但是我被明确告知我也不应该在这里使用“new”运算符。我只是想从项目中“拉出”实际模型,然后扩展它。
有什么办法吗?还是这超出了 Javascript 的能力范围?
用于“命名空间”的代码是这样的;
(function (global) {
var globalNamespace = global['website'];
var VERSION = '3.0.1';
function Module() { }
function numeric(s) {
if (!s) {
return 0;
}
var a = s.split('.');
return 10000 * parseInt(a[0]) + 100 * parseInt(a[1]) + parseInt(a[2]);
}
if (globalNamespace) {
if (numeric(VERSION) <= numeric(globalNamespace['VERSION'])) {
return;
}
Module = globalNamespace.constructor;
} else {
global['website'] = globalNamespace = new Module();
}
globalNamespace['VERSION'] = VERSION;
function require(path) {
path = path.replace(/-/g, '_');
var parts = path.split('.');
var ns = globalNamespace;
for (var i = 0; i < parts.length; i++) {
if (ns[parts[i]] === undefined) {
ns[parts[i]] = new Module();
}
ns = ns[parts[i]];
}
return ns;
}
var proto = Module.prototype;
proto['module'] = function (path, closure) {
var exports = require(path);
if (closure) {
closure(exports, require);
}
return exports;
};
proto['extend'] = function (exports) {
for (var sym in exports) {
if (exports.hasOwnProperty(sym)) {
this[sym] = exports[sym];
}
}
};
}(this));
最佳答案
由于您的对象包含嵌套对象,因此您需要使用深度扩展。为此,将 true
添加为 $.extend
的第一个参数。
exports.schema = $.extend(true, {
Item: {
Id: null, // will be filled in on the UI
Name: null, // will be filled in on the UI
Description: null, // will be filled in on the UI
Timestamp: null // will be filled in on the UI
},
Editing: false
}, website.admin.models.item);
当您添加 kendoui 时,(我不会说不可能,可能有一种我没有想到的方法)以这种方式扩展它变得困难,并且更容易只需在模块外创建一个对象并扩展它。
var theObj = {
...
};
(function ($, window, kendo) {
ehrpg.module('admin.models', function (exports, require) {
exports.item = kendo.data.Model.define($.extend(true,{},theObj));
});
})(jQuery, window, kendo);
(function ($, window, kendo) {
ehrpg.module('admin.models', function (exports, require) {
exports.schema = kendo.data.Model.define($.extend(true,{
Item: {
Id: null,
Name: null,
Dirty: false,
Timestamp: null
}
},theObj));
});
})(jQuery, window, kendo);
关于Javascript : "extend" an object without an instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481588/
我知道我的问题有点含糊,但我不知道如何描述它。我问过很多地方,但似乎没有人理解我为什么要这样做。但请耐心等待,我会解释为什么我想要这样的东西。 我使用 Liquid Templates 允许用户在我的
这个问题在这里已经有了答案: what is the difference between null != object and object!=null [duplicate] (2 个回答) 7年
当我在我的本地主机 Google App Engine 应用程序中将日志记录级别更改为 FINE 时,我开始在我的跟踪堆栈中看到这些: Apr 17, 2013 4:54:20 PM com.goog
Python 有内置函数 type : class type(object) With one argument, return the type of an object. The return v
我正在使用深度学习进行语义分割,我遇到了以下术语:语义分割、实例检测、对象检测 和对象分割. 它们有什么区别? 最佳答案 这些术语的某些用法对用户而言是主观的或依赖于上下文,但据我所知对这些术语的合理
我面临 -[NSConcreteMutableData release] 的问题:消息发送到已释放的实例,我也附上了我的示例代码。 - (IBAction)uploadImage { NSString
我试图显示模型中的单个实例(数据库行),其中多个实例共享多行的相同字段(列)值。为了澄清这一说法,我有以下情况: ID/Title/Slug/Modified 1 Car A 1s ag
我正在尝试使用mockito来模拟服务。然而,我没有找到一种方法来告诉mockito,给定一个类的实例返回给我相同的实例: 类似于: given(service.add(any(Individua
我知道如何从父类(super class)原型(prototype)创建子类原型(prototype)。但是,如果我已经有了父类(super class)对象的实例来创建子类对象怎么办? 在 JS 中
鉴于 Kotlin 1.1。对于某个类的 instance,instance::class.java 和 instance.javaClass 似乎几乎是等价的: val i = 0 println(
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Find out the instance id from within an ec2 machine 我正在寻找从
为什么我的 Instantiate 函数没有创建 That 的“空白”实例? 我有以下最小类: classdef That < handle properties This = '' end
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTra
考虑以下几点: public class A { public String name = "i am a A instance"; } public class B extends A {
我正在使用 Scalr 来扩展网站服务器。 在 Apache 服务器上,我安装了 Sakai,并为 Linux 机器创建了一个启动脚本。 问题是,如何确保MySQL实例在Apache服务器启动之前启动
Android Realm DB 允许使用 Realm.getInstance() 获取多个实例。这些中的最佳实践是什么? :1.创建单个实例(应用程序范围)并在任何地方使用它2. 在需要时获取一个新
我很难理解为什么修改实例 a 中的属性会修改实例 b 中的相同属性。 var A = function (){ }; A.prototype.data = { value : 0 }; var
我将 Weka 用作更长管道的一部分,因此,我无法承受将所有数据写入文件或数据库只是为了创建一个 Instances。目的。我可以即时做的是创建 Instance 的列表对象。 来自 this pag
class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func = c.func # c.func i
Angular Routing 文档提到了组件实例创建、组件实例激活和路由激活。 文档没有解释这些概念的区别,以及每次创建/激活发生的时间。 问题 实例创建和实例激活有什么区别? 实例激活和路由激活有
我是一名优秀的程序员,十分优秀!