Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

预防Model和View中原型上有引用类型属性因共享修改导致的问题 #172

Merged
merged 5 commits into from
May 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define(
function (require) {
var util = require('./util');
var Deferred = require('./Deferred');
var _ = require('underscore');

var SILENT = {silent: true};

Expand Down Expand Up @@ -196,6 +197,12 @@ define(
this.store = {};
this.pendingWorkers = [];

// 如果prototype上的属性是引用类型,则复制一份,
// 防止因共享修改导致的问题
if (!this.hasOwnProperty('datasource') && this.datasource) {
this.datasource = _.clone(this.datasource);
}

if (context) {
this.fill(context, SILENT);
}
Expand Down
12 changes: 12 additions & 0 deletions src/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
define(
function (require) {
var util = require('./util');
var _ = require('underscore');

/**
* @class View
Expand All @@ -26,6 +27,17 @@ define(
var exports = {};

exports.constructor = function () {

// 如果prototype上的属性是引用类型,则复制一份,
// 防止因共享修改导致的问题
if (!this.hasOwnProperty('uiProperties') && this.uiProperties) {
this.uiProperties = _.clone(this.uiProperties);
}

if (!this.hasOwnProperty('uiEvents') && this.uiEvents) {
this.uiEvents = _.clone(this.uiEvents);
}

this.initialize();
};

Expand Down
8 changes: 4 additions & 4 deletions test/spec/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(function() {
var util = require('er/util');

describe('util', function() {
it('should export `guid` method', function() {
expect(util.guid).toBeOfType('function');
Expand Down Expand Up @@ -33,7 +33,7 @@ define(function() {
});
describe('`mix` method', function() {
it('should return the same object C', function() {
var a = {
var a = {
"a": 1
},
b = {
Expand All @@ -47,13 +47,13 @@ define(function() {
expect(util.mix(a, b)).not.toBe(c);
});
});

describe('`bind` method', function() {
it('should accept a function', function() {
expect(function() { util.bind(function(){}) }).not.toThrow();
});
});

describe('`inherits` method', function() {
it('classA should be inherit superClassA', function() {
function SuperClassA() {}
Expand Down