2014-10-11 41 views
1

從在JavaScript this link,海關元件延伸按鈕被製成爲:extendTag在達特定製元素

var MegaButton = document.registerElement('mega-button', { 
    prototype: Object.create(HTMLButtonElement.prototype), 
    extends: 'button' 
}); 

<button is="mega-button"> 

我試圖使所述使用鏢相同,通過這樣的代碼:

class MegaButton extends ButtonElement { 
static final tag = 'mega-button'; 
factory MegaButton()=>new Element.tag('button', tag); 
    MegaButton.created() : super.created() { 
     var shadow = this.createShadowRoot(); 
     shadow.text='save'; 
    } 
} 
document.registerElement(MegaButton.tag, MegaButton); 

在html文件

<button is="mega-button"></button> 
    <mega-button>click me</mega-button> 

但得到了s錯誤:

Exception: Unsupported operation: Class must provide extendsTag if base native class is not HTMLElement 

任何幫助請。感謝

+0

哪條線導致異常? – 2014-10-11 15:48:00

+0

document.registerElement(MegaButton.tag,MegaButton);這在一個單獨的函數中被調用,我註冊了所有的自定義元素。 – 2014-10-11 15:54:39

回答

1

document.registerElement應該是這樣的:

document.registerElement(MegaButton.tag, MegaButton, extendsTag: 'button'); 
=> new Element.tag('button', tag); 

也看到Custom Polymer element extending AElement in Dart

+0

沒有工作:(我不想使用聚合物自定義元素,只是DART香草自定義元素。我調整了我的問題中的代碼,以反映您的答案後我的代碼。是否有一些東西在項目註冊錯誤! – 2014-10-11 15:45:49

+0

'Element.tag'是純粹的'dart:html'。 – 2014-10-11 15:47:13

+0

我更新了我的答案。 – 2014-10-11 15:58:12

0

下面的代碼與我的工作完美:

class SaveBtn extends HtmlElement { 
     static final tag = 'save-button'; 
     factory SaveBtn()=>new Element.tag(tag); 

    SaveBtn.created() : super.created() { 
    // Create a Shadow Root 
    var shadow = this.createShadowRoot(); 
    // Create a standard element and set it's attributes. 
    var btn = new ButtonElement() 
    ..style.height= '20px' 
    ..style.width= '50px' 
    ..style.color= '#FF8F66' 
    ..style.border='1px solid #BCC1C8' 
    ..style.background='#F1F4FB' 
    ..style.borderRadius='5px' 
    ..style.fontFamily='openSansItalic' 
    ..style.fontSize='12px' 
    ..style.padding='0px 6px' 
    ..style.marginLeft='0.1em' 
    ..style.borderBeforeStyle='solid' 
    ..style.borderWidth='1px' 
    ..style.borderColor='transparent' 
    ..style.marginBottom='2px' 
    ..style.borderBottom='1px solid #D1DBE9'; 

    btn.text= this.getAttribute('data-name'); 

    btn.onMouseDown.listen((e){ 
     btn..style.color="#333" 
    ..style.background='#FF8F66'; 
    }); 
    btn.onMouseUp.listen((e){ 
    btn..style.color="#FF8F66" 
    ..style.background='#F1F4FB' 
    ..style.outline='none'; // remove the focus outline/glur 
    }); 
    btn.onMouseEnter.listen((e)=> btn..style.boxShadow='0px 0px 5px #888888'); 
    btn.onMouseLeave.listen((e)=> btn..style.boxShadow='0px 0px 0px'); 

    if(btn.disabled==true){ 
    btn..style.color="gray"; 
    } 
    shadow.nodes.add(btn); 

    Element launchElement(){ 
    return (shadow); 
    } 

    } 
} 

自定義元素註冊:

document.registerElement(SaveBtn.tag, SaveBtn); 

,並在HTML文件中,我使用:

<save-button data-name='save orders'></save-button> 
+0

當你添加自己的答案時,當它包含額外的信息,但是當我提供的信息導致解決方案時,我希望至少有一個滿意的答案,尤其是當它不是一個微不足道的問題時,它是完全正確的。 – 2014-10-12 12:13:11