【问题标题】:Access Aurelia custom attribute function from within nested HTML elements从嵌套的 HTML 元素中访问 Aurelia 自定义属性函数
【发布时间】:2017-01-01 06:21:24
【问题描述】:

我有一个视图/视图模型对,它实现了弹出框自定义属性。我的具体目标包括在弹出框本身内单击按钮以及单击页面上的其他任何位置时关闭弹出框。

我可以让我的 doSomething() VM 函数在视图中的元素级别上工作,但不能在包含元素的属性内工作。我已经在代码 cmets 中进一步解释了这个问题。我会很感激一些指导。谢谢!

blog.html

<template>
<require from="../popover/popover"></require>

...

<!-- doSomething() works here! -->
<button type='button' click.trigger='doSomething()'>ClickMe</button>

<!-- doSomething() does not work here! -->
<!-- `click.trigger`/`click.delegate` does not trigger anything, while `onclick` shows
     "Uncaught ReferenceError: doSomething is not defined" -->
<span class="glyphicon glyphicon-star" popover="title.bind: blogpost.title; placement.bind: 'top'" 
data-content='<button type="button" click.trigger="doSomething()">ClickMe</button>' ></span>

...
</template>

blog.ts

... 
doSomething() {
    console.log('doing something');
}

popover.ts

...
bind() {
$(this.element).popover({
    title: this.title,
    placement: this.placement,
    content: this.content,
    trigger: 'click',
    html: true
    });
}

【问题讨论】:

  • 你用的是什么popover插件?
  • @AshleyGrant Twitter Bootstrap,我大致遵循了这个:sitepoint.com/extending-html-aurelia-io-way
  • 我在想 TemplatingEngine 可以用来提供弹出按钮 aurelia 绑定功能?
  • 我要问问 Jeremy Danyow 能否在这方面为您提供帮助。他是我们的装订向导。
  • 太棒了,谢谢@AshleyGrant!老实说,这可能很简单。我还尝试弄乱如何添加弹出框的内容,因为我认为这可能是错误的根源......上面我使用了data-content 属性,而&lt;span class="glyphicon glyphicon-star" popover="title.bind: blogpost.title; placement.bind: 'top'; content.bind: 'my content';"&gt;&lt;/span&gt; 也可以使用。当我尝试在上面使用 html 而不是 my content 时,出现错误:Uncaught TypeError: this.sourceExpression.connect is not a function

标签: html mvvm data-binding typescript aurelia


【解决方案1】:

我最近遇到了同样的问题。我目前正在为 Aurelia 开发一个 Bootstrap 端口,它还没有完成,我还没有写任何文档,但是弹出框已经实现了。

非常欢迎您来看看: https://github.com/tochoromero/aurelia-bootstrap/tree/master/src/popover

您尝试实现弹出框的方式将非常复杂(如果可能的话),范围会变得混乱。如果您只有文本,那很好,但您基本上希望能够将任何内容放入弹出框并将其绑定到正确的 View-Model。

我解决这个问题的方法是使用几个自定义元素来表示弹出框,我有 AubsCustomPopover、AubsPopoverTitle 和 AubsPopoverContent。 使用这 3 个自定义元素,您将为弹出框创建引导标记,并且因为您将它们直接添加到视图中,它们将具有正确的视图模型,您可以在其中做任何您想做的事情。

然后,有一个自定义属性,这就是 AubsPopover,它负责根据您指定的触发操作(悬停、单击、聚焦、外部单击)显示和隐藏自定义属性。

使用它的代码如下所示:

<aubs-custom-popover model.bind="customPopover">
  <aubs-popover-title>
     This is my awesome title <i class="fa fa-star"></i>
  </aubs-popover-title>
  <aubs-popover-content>
    <div class="form-group">
      <label for="exampleInputPassword1">Password</label>
      <input type="password" class="form-control" text.bind="password"   
             placeholder="Password">
    </div>

    <button class="btn btn-default" click.delegate="isOpen = false">
      Close
    </button>
  </aubs-popover-content>
</aubs-custom-popover>

<button class="btn btn-primary" 
     aubs-popover="custom-model.bind: customPopover; 
                   trigger: outsideClick; 
                   position: bottom;   
                   open.bind:isOpen">
     Custom Popover
</button>

正如我所说的那样,Popover 已经完全实现,但我还没有编写任何文档,在我这样做之前我希望有更多的组件。

如果你想使用它,我可以帮你设置它。

【讨论】:

  • 酷,谢谢!稍后我将研究代码并尝试一下。欢迎任何设置帮助!
  • 您基本上可以复制popover文件夹中的类并将它们包含在您的应用程序中,然后根据我提供的示例创建您的popover。我希望我能尽快让 bootstrap 项目达到一个好的状态,这样我就可以编写文档了
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多