Skip to content

Commit

Permalink
Merge pull request #172 from The-only/master
Browse files Browse the repository at this point in the history
预防Model和View中原型上有引用类型属性因共享修改导致的问题
  • Loading branch information
otakustay committed May 17, 2016
2 parents 917790d + a3f3bbb commit 6f0a0bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
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

0 comments on commit 6f0a0bd

Please sign in to comment.