2016-10-06 27 views
1

我已經開始了Aurelia項目,並且選擇使用this plugin以使用類似於this post中所見的實現方式爲電話號碼啓用輸入掩碼。jQuery插件僅在Firefox中與Aurelia無關

它可以在Chrome和Safari中正常工作 - 但是,它只是在Firefox中無法正常工作。沒有錯誤或其他有用的信息。上述聯演示頁的工作例子就好了,但是,我敢肯定,這一定有什麼我的執行:

JS:

import {inject, NewInstance} from 'aurelia-dependency-injection'; 
import {ValidationController, ValidationRules, validateTrigger} from 'aurelia-validation'; 
import 'igorescobar/jQuery-Mask-Plugin' 

@inject(NewInstance.of(ValidationController)) 

export class MyForm { 
    async activate(params, routeConfig) { 
    // do stuff if there are route parameters 
    } 

    bind() { 
    $('#PhoneNumber').mask('(000) 000-0000'); 
    } 

    constructor(controller) { 
    this.controller = controller; 
    this.controller.validateTrigger = validateTrigger.manual; 
    } 

    submit() { 
    this.controller.validate() 
     .then(errors => { 
     if (errors.length === 0) { 
      // do a thing 
     } else { 
      // do something else 
     } 
     }); 
    } 
} 

ValidationRules 
    .ensure('phoneNumber').displayName('Phone number') 
    .minLength(10).withMessage('Phone number must be at least 10 characters') 
    .maxLength(14).withMessage('Phone number is too long') 
    .matches(/[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}/).withMessage('Please provide a valid phone number') 
    .on(MyForm); 

HTML

<input class="form-control" id="PhoneNumber" type="tel" minlength="10" maxlength="14" pattern="[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}" value.bind="phoneNumber & validate" required="required"> 

我試過刪除模式屬性,並將其更改爲常規文本輸入,無濟於事。我真的在這個問題上撓頭。任何想法或建議將不勝感激。

回答

2

我和Jan一起工​​作 - 我們從來沒有在Firefox中以原始問題編碼的方式工作。

因爲我們遇到了與jquery.mask('igorescobar/jQuery-Mask-Plugin')的兼容性問題,我們最終將其替換爲jquery.inputmask。

下面是使它工作(基於Two-Way binding in an Aurelia Custom Attribute)膠:

輸入mask.js

import {inject, customAttribute, bindable, bindingMode} from 'aurelia-framework'; 
import 'jquery.inputmask'; 
import 'jquery.inputmask.numeric'; 

@customAttribute('input-mask') 
@inject(Element) 
export class InputMaskCustomAttribute { 

    @bindable({defaultBindingMode: bindingMode.twoWay}) unmaskedValue; 

    @bindable maskOptions; 

    element; 

    isValidInputElement; 
    // The maskedinput jquery pluggin does not allow the events that aurelia needs 
    // for binding to get through. So we will manually put them through. 
    // This is the list of events we are going to look for. "focusout" allows for the value 
    // to be correct intime for "onblur". 
    aureliaBindEvents = 'focusout'; 
    eventTarget = undefined; 

    constructor(element) { 
    if (element instanceof HTMLInputElement) { 
     this.element = element; 
     this.isValidInputElement = true; 
     this.eventTarget = $(this.element); 
    } else { 
     this.isValidInputElement = false; 
    } 
    } 

    bind() { 
    this.element.value = this.unmaskedValue; 
    } 

    attached() { 
    this.setupMask(); 
    } 

    detached() { 
    this.tearDownMask(); 
    } 

    maskOptionsChanged(newValue, oldValue) { 
    this.tearDownMask(); 
    this.setupMask(); 
    } 

    setupMask(mask) { 
    if (this.isValidInputElement && this.maskOptions) { 
     this.eventTarget.inputmask(this.maskOptions); 
     this.eventTarget.on(this.aureliaBindEvents, (e) => { 
     this.unmaskedValue = this.eventTarget.inputmask('unmaskedvalue'); 
     this.fireEvent(e.target, 'input'); 
     }); 
    } 
    } 

    tearDownMask() { 
    if (this.isValidInputElement) { 
     if (this.eventTarget) { 
     this.eventTarget.off(this.aureliaBindEvents); 
     this.eventTarget.inputmask('remove'); 
     } 
    } 
    } 

    createEvent(name: string) { 
    let event = document.createEvent('Event'); 
    event.initEvent(name, true, true); 
    return event; 
    } 

    fireEvent(element: HTMLElement, name: string) { 
    var event = this.createEvent(name); 
    element.dispatchEvent(event); 
    } 

} 

widget.html

<require from="input-mask"></require> 
<input class="form-control" type="text" maxlength="12" 
    input-mask="mask-options.bind: {alias: 'currency', rightAlign: false, allowMinus:false, allowPlus: false}; unmasked-value.bind: target['monthly-payment'] & validate" /> 
+0

我不明白。如果我複製代碼,那麼當我將鼠標懸停(或聚焦)到輸入元素上時,會顯示單詞currency,但輸入不可編輯,這意味着它不響應輸入任何值。 –

+0

我認爲這是因爲您確實在您的依賴關係管理器中使用了jquery.inputmask.numeric.js。 以下是我如何使用JSPM(在config.js中的'map'鍵中): '「jquery.inputmask」:「npm:[email protected]」, 「jquery.inputmask.numeric」 :「npm:[email protected] /dist/inputmask/inputmask.numeric.extensions.js」,' – antonmos

+0

我實際上還有其他東西......感謝您的幫助:) –

0

您可以在attached()事件(在html呈現之後)上使用jquery命令,而不是在bind()上使用jquery命令。試試看:

attached() { 
    $('#PhoneNumber').mask('(000) 000-0000'); 
} 
+0

仍然沒有按」 t似乎在Firefox中工作(Chrome雖然很好)。任何其他想法? – jszpila

+0

在Firefox中按下「CTRL + SHIFT + J」可查看所有JavaScript控制檯中的錯誤日誌以獲取有關問題的更多信息。你能在那裏看到任何嫌疑犯嗎? – JayDi

+0

標準Aurelia開發調試語句加上console.log的輸出我把附加的方法,以確保它被解僱 - 沒有錯誤。 Chrome和FF之間的調試輸出似乎是一樣的,所以沒有任何東西似乎加載不同。 – jszpila