-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Two-way sync between rows and model (#167)
- Loading branch information
1 parent
df72ead
commit 0ec6c36
Showing
11 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Ember from 'ember'; | ||
import config from 'ember-get-config'; | ||
|
||
const assign = Ember.assign || Ember.merge; | ||
const globalOptions = config['ember-light-table'] || {}; | ||
|
||
export default globalOptions; | ||
|
||
export function mergeOptionsWithGlobals(options) { | ||
return assign(assign({}, globalOptions), options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Ember from 'ember'; | ||
|
||
const { | ||
assert, | ||
isArray | ||
} = Ember; | ||
|
||
export default Ember.ArrayProxy.extend({ | ||
/** | ||
* The model that will be synchronized to the content of this proxy | ||
* @property syncArray | ||
* @type {Array} | ||
*/ | ||
syncArray: null, | ||
|
||
/** | ||
* @property syncEnabled | ||
* @type {Boolean} | ||
*/ | ||
syncEnabled: true, | ||
|
||
init() { | ||
this._super(...arguments); | ||
|
||
let syncArray = this.get('syncArray'); | ||
|
||
assert('[ember-light-table] enableSync requires the passed array to be an instance of Ember.A', isArray(syncArray) && typeof syncArray.addArrayObserver === 'function'); | ||
|
||
syncArray.addArrayObserver(this, { | ||
willChange: 'syncArrayWillChange', | ||
didChange: 'syncArrayDidChange' | ||
}); | ||
}, | ||
|
||
destroy() { | ||
this.get('syncArray').removeArrayObserver(this, { | ||
willChange: 'syncArrayWillChange', | ||
didChange: 'syncArrayDidChange' | ||
}); | ||
|
||
this.setProperties({ syncArray: null, content: null }); | ||
}, | ||
|
||
/** | ||
* Serialize objects before they are inserted into the content array | ||
* @method serializeContentObjects | ||
* @param {Array} objects | ||
* @return {Array} | ||
*/ | ||
serializeContentObjects(objects) { | ||
return objects; | ||
}, | ||
|
||
/** | ||
* Serialize objects before they are inserted into the sync array | ||
* @method serializeSyncArrayObjects | ||
* @param {Array} objects | ||
* @return {Array} | ||
*/ | ||
serializeSyncArrayObjects(objects) { | ||
return objects; | ||
}, | ||
|
||
syncArrayWillChange() { /* Not needed */}, | ||
|
||
syncArrayDidChange(syncArray, start, removeCount, addCount) { | ||
let content = this.get('content'); | ||
|
||
if(!this.get('syncEnabled')) { | ||
return; | ||
} | ||
|
||
if(addCount > 0) { | ||
content.replace(start, 0, this.serializeContentObjects(syncArray.slice(start, start + addCount))); | ||
} else if(removeCount > 0) { | ||
content.replace(start, removeCount, []); | ||
} | ||
}, | ||
|
||
replaceContent(start, removeCount, objectsToAdd) { | ||
let syncArray = this.get('syncArray'); | ||
|
||
if(!this.get('syncEnabled')) { | ||
return this._super(...arguments); | ||
} | ||
|
||
syncArray.replace(start, removeCount, this.serializeSyncArrayObjects(objectsToAdd)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters