-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1604 from Jinjiang/jsfm-feature-component-methods
[jsfm] support component methods in another way
- Loading branch information
Showing
6 changed files
with
159 additions
and
9 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
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,57 @@ | ||
import { getListener } from './operation' | ||
|
||
let Element | ||
|
||
export function setElement (El) { | ||
Element = El | ||
} | ||
|
||
/** | ||
* A map which stores all type of elements. | ||
* @type {Object} | ||
*/ | ||
export const elementTypes = {} | ||
|
||
/** | ||
* Register an extended element type with component methods. | ||
* @param {string} type component type | ||
* @param {array} methods a list of method names | ||
*/ | ||
export function registerElement (type, methods) { | ||
// Skip when no special component methods. | ||
if (!methods || !methods.length) { | ||
return | ||
} | ||
|
||
// Init constructor. | ||
const XElement = function (props) { | ||
Element.call(this, type, props, true) | ||
} | ||
|
||
// Init prototype. | ||
XElement.prototype = Object.create(Element.prototype) | ||
XElement.prototype.constructor = Element | ||
|
||
// Add methods to prototype. | ||
methods.forEach(methodName => { | ||
XElement.prototype[methodName] = function (...args) { | ||
const listener = getListener(this.docId) | ||
if (listener) { | ||
listener.callComponentMethod(this.ref, type, methodName, args) | ||
} | ||
} | ||
}) | ||
|
||
// Add to element type map. | ||
elementTypes[type] = XElement | ||
} | ||
|
||
/** | ||
* Clear all element types. Only for testing. | ||
*/ | ||
export function clearElementTypes () { | ||
for (const type in elementTypes) { | ||
delete elementTypes[type] | ||
} | ||
} | ||
|
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