2017-02-06 1 views
1

DOMに1つ以上の一時要素を作成するビューモデルトリガーを持つことができます。これらの要素は、一定期間後に削除されます。Aureliaでは、一時的な要素と動的な要素を追加する正しい方法は何ですか

これは、後の効果の簡単な例についてはfiddleを参照してください。ここで

はそれを行うための簡単なjQueryの方法です:

var count = 1; 
$("button").bind("click", function(e) { 
    createNewNotifier(count++); 
}); 

function createNewNotifier(text) { 
    var div = $('<div></div>').appendTo(".notifications"); 
    div.text("Notification: " + text); 
    div.hide(4000, function(){ $target.remove(); }); 
} 
+0

私は個人的にこの状況を処理するためにトーストを使用すると言います。たとえそれがAureliaコンポーネントでなくても、 "うまくいく"という意味です。 –

答えて

2

あなたはちょうどあなたがに通知を発行した通知の配列持つことができます - いくつかのビューに

export class Notifier { 
    notifications = []; 
    addNotification(notification) { 
    this.notifications.push(notification); 
    setTimeout(() => { 
     let index = this.notifications.indexOf(notification); 
     notifications.splice(index, 1); 
    } 
} 

を -

<template> 
    <ul> 
    <li repeat.for="notification of notifier.notifications">${notification.value}</li> 
    </ul> 
</template> 

とビューモデル

import {Notifier} from './notifier'; 
    export class ViewModel { 
    static inject = [Notifier]; 
    constructor(notifier) { 
     this.notifier = notifier; 
    } 
    attached() { 
     this.notifier.add({ value: 'Attached!' }); 
    } 
    } 
+0

アレイが変更されると、すべての通知が削除され、読み込まれますか?もしそうなら、それは進行中のアニメーションに影響しますか? – efunneko

+0

新しい配列を割り当てると、それが起こるかもしれませんが、 'push'、' pop'、 'splice'のような操作を使っているだけで、配列を操作していれば、それらは削除されません。読んだ。 –

+0

少しの実験の後、Aureliaの 'enter'アニメーションを使用している限り記述されていることが分かりました。私がアニメーションを「離れる」とすると、アニメーションが実行されている間、テンプレートは新しい通知要素を追加しないように見えます。おそらくバグ? – efunneko

関連する問題