首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >泛型Aurelia对话框需要在脏和有效时启用保存

泛型Aurelia对话框需要在脏和有效时启用保存
EN

Stack Overflow用户
提问于 2017-01-13 14:32:21
回答 1查看 269关注 0票数 1

我试着做一个通用的,可重用的对话框,我们可以用来在简单的对象上输入基本数据--电话、电子邮件等等。

通常,我有一个helper类来打开对话框,一个使用aurelia-对话框的泛型对话框,然后是一个vm,它被组成为包含所有表单编辑代码的主体的泛型对话框。

我遇到的问题是,按钮在页脚中,这是对话框vm的一部分,但是在组成主体的vm中存在验证和脏指示符。我希望根据主体vm中的指示符禁用/启用Save按钮(在脚注中)。

对话框助手:

代码语言:javascript
运行
复制
import {DialogService} from 'aurelia-dialog'
import {EditDialog} from 'app/resources/dialogs/edit-dialog'

export class DialogHelper {
    static inject = [DialogService] 

    constructor(dialogService){
        this.dialogService = dialogService
    }

    edit(title, view, item, showDelete){
        return this.dialogService.open({
             viewModel: EditDialog, 
             model: {view: view, 
                     item: item, 
                    title: title, 
               showDelete: showDelete }  })
    }
}

呼叫代码:

代码语言:javascript
运行
复制
let email = {emailAddress: "123@gmail.com", emailType: "Personal"}
this.dialogHelper.edit("Email",'./pages/email-dialog',email,false)

EditDialog.js

代码语言:javascript
运行
复制
import {DialogController} from 'aurelia-dialog'

export class EditDialog {
   static inject = [DialogController]

   showDelete = false

   constructor(dialogController){
        this.dialogController = dialogController
   }

   activate(options){
       this.options = options
   }

EditDialog.html

代码语言:javascript
运行
复制
<template>
    <ai-dialog>
       <ai-dialog-header style="display:flex; justify-content:space-between">
          <span>Add/Edit ${options.title}</span><i style="cursor:pointer" class="fa fa-close" click.delegate="dialogController.cancel()"></i>
       </ai-dialog-header>

       <ai-dialog-body>
           <compose view-model.bind="options.view" model.bind="options.item" ></compose>
       </ai-dialog-body>

       <ai-dialog-footer style="display:flex;justify-content:space-between;">
        <span>
<!--Should have an if.bind here but not sure how since vm composed above knows details -->
            <button click.delegate="dialogController.ok({type:'save'})">Save</button>
            <button click.delegate="dialogController.cancel()">Cancel</button>
        </span>
        <button if.bind="options.showDelete" click.delegate="dialogController.ok({type:'delete'})"><i class="fa fa-trash"></i></button>
       </ai-dialog-footer>
   </ai-dialog>
</template>

email-dialog.js

代码语言:javascript
运行
复制
import {ValidationRules, ValidationController} from 'aurelia-validation'
import {computedFrom,NewInstance} from 'aurelia-framework'

export class EmailDialog {
    static inject = [NewInstance.of(ValidationController)]
    email = null

    constructor(validationController){
       this.validationController = validationController
       this.rule = ValidationRules.ensure(i => i.emailAddress)
                    .required()
                    .email()
                  .ensure(i => i.emailType)
                     .required()
                  .rules
    }

    activate(item){
       this.original = item
       this.email = JSON.parse(JSON.stringify(item))
    }

    @computedFrom('email.emailAddress', 'email.emailType')
    get canSave() {
        for(var prop in this.original){
            if(this.email[prop] != this.original[prop])
                 return false
        }

        return true
    }

}

我是否必须使用EventAggregator将消息传递给父消息,或者是否有更好的方法通过某种绑定来处理此问题?(或者有没有一个更好的方法来共同解决这个问题?)

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-13 16:11:35

我不完全确定这是否适用于aurelia-对话框,但我的建议是:

您可以使用view-model.ref="someIdentifier"属性获取<compose>元素视图模型的引用。备忘单

这个引用应该是一个包含实际视图模型实例(例如,Compose )的currentViewModel属性的currentViewModel类。

下面是一个简化的要点示例:https://gist.run/?id=b23bf709d98b3bd3ae427852f104c890

将其应用于你的案件:

代码语言:javascript
运行
复制
<ai-dialog-body>
    <compose view-model.ref="customVM" view-model.bind="options.view" model.bind="options.item" ></compose>
</ai-dialog-body>

<ai-dialog-footer style="display:flex;justify-content:space-between;">
    <span>
        <button if.bind="customVM.currentViewModel.canSave" click.delegate="dialogController.ok({type:'save'})">Save</button>
        <button click.delegate="dialogController.cancel()">Cancel</button>
    </span>
    <button if.bind="options.showDelete" click.delegate="dialogController.ok({type:'delete'})"><i class="fa fa-trash"></i></button>
</ai-dialog-footer>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41636905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档