Skip to content

feat: add support for angular two way data binding #706

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

Merged
merged 12 commits into from
Aug 21, 2019
5 changes: 2 additions & 3 deletions packages/base/src/UI5Element.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import boot from "./boot.js";
import { getNoConflict } from "./config/NoConflict.js";
import { skipOriginalEvent } from "./config/NoConflict.js";
import { getCompactSize } from "./config/CompactSize.js";
import DOMObserver from "./compatibility/DOMObserver.js";
import UI5ElementMetadata from "./UI5ElementMetadata.js";
Expand Down Expand Up @@ -522,7 +522,6 @@ class UI5Element extends HTMLElement {
*/
fireEvent(name, data, cancelable) {
let compatEventResult = true; // Initialized to true, because if the event is not fired at all, it should be considered "not-prevented"
const noConflict = getNoConflict();

const noConflictEvent = new CustomEvent(`ui5-${name}`, {
detail: data,
Expand All @@ -534,7 +533,7 @@ class UI5Element extends HTMLElement {
// This will be false if the compat event is prevented
compatEventResult = this.dispatchEvent(noConflictEvent);

if (noConflict === true || (noConflict.events && noConflict.events.includes && noConflict.events.includes(name))) {
if (skipOriginalEvent(name)) {
return compatEventResult;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/base/src/config/NoConflict.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import { getNoConflict as getConfiguredNoConflict } from "../InitialConfiguration.js";

// Fire these events even with noConflict: true
const excludeList = [
"value-changed",
];

const shouldFireOriginalEvent = eventName => {
return excludeList.includes(eventName);
};

let noConflict = getConfiguredNoConflict();

const shouldNotFireOriginalEvent = eventName => {
return !(noConflict.events && noConflict.events.includes && noConflict.events.includes(eventName));
};

const getNoConflict = () => {
return noConflict;
};

const skipOriginalEvent = eventName => {
// Always fire these events
if (shouldFireOriginalEvent(eventName)) {
return false;
}

// Read from the configuration
if (noConflict === true) {
return true;
}

return !shouldNotFireOriginalEvent(eventName);
};

const setNoConflict = noConflictData => {
noConflict = noConflictData;
};

export {
getNoConflict,
setNoConflict,
skipOriginalEvent,
};
2 changes: 2 additions & 0 deletions packages/main/src/CheckBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class CheckBox extends UI5Element {
if (this.canToggle()) {
this.checked = !this.checked;
this.fireEvent("change");
// Angular two way data binding
this.fireEvent("value-changed");
}
return this;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ class DatePicker extends UI5Element {

this.value = nextValue;
this.fireEvent("change", { value: nextValue, valid: isValid });
// Angular two way data binding
this.fireEvent("value-changed", { value: nextValue, valid: isValid });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check no-conflict mode (should not fire ui5- for value-changed)

}

_handleInputLiveChange() {
Expand Down Expand Up @@ -425,6 +427,8 @@ class DatePicker extends UI5Element {
this.closePicker();

this.fireEvent("change", { value: this.value, valid: true });
// Angular two way data binding
this.fireEvent("value-changed", { value: this.value, valid: true });
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ class Input extends UI5Element {

if (isUserInput) { // input
this.fireEvent(this.EVENT_INPUT);
// Angular two way data binding
this.fireEvent("value-changed");
return;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ class Switch extends UI5Element {
if (!this.disabled) {
this.checked = !this.checked;
this.fireEvent("change");
// Angular two way data binding;
this.fireEvent("value-changed");
}
}

Expand Down