使用列表的常见用例是从列表项中访问列表的方法.例如:项目项具有从包含列表中删除自身的选项.我想知道我在下面描述的Aurelia模式是否有效,或者是否有更好的解决方案.
在Aurelia,我有以下设置:
包含列表:(project-list.html和projectList.js)
<template>
<div class="projects">
<input value.bind="newProjectName" placeholder="New project name"/>
<project-item repeat.for="project of projects" project.bind="project"></project-item>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
和子项:( project-item和projectItem.js)
<template>
<span class="title">
${project.name} <i click.delegate="deleteProject(project)" class="icon-trash"></i>
</span>
</template>
Run Code Online (Sandbox Code Playgroud)
在这种情况下deleteProject(project)是projectList VM的成员:
function deleteProject(project){
var index = this.projects.indexOf(project);
if (index>-1){
this.projects.splice(index,1)
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,正如我从这个问题中理解的那样https://github.com/aurelia/framework/issues/311,这将不再起作用.
作为解决方法,我可以在项目项VM上绑定一个函数:
@bindable delete: Function;
Run Code Online (Sandbox Code Playgroud)
并在项目列表模板中:
<project-item repeat.for="project of projects" project.bind="project" delete.bind="deleteProject"></project-item>
Run Code Online (Sandbox Code Playgroud)
这确实有效,提供绑定函数是带有闭包的赋值属性:
deleteProject = function(project : Projects.Project){
var index = this.projects.indexOf(project);
if (index>-1){
_.remove(this.projects,(v,i)=>i==index);
}
}
Run Code Online (Sandbox Code Playgroud)
需要闭包来访问正确的上下文(this作为项目列表).运用
function deleteProject(project)
Run Code Online (Sandbox Code Playgroud)
this 将引用项目项的上下文. …
使用Aurelia我正在努力使用绑定和repeat.for:假设我在viewmodel中有一个属性menuItems(一个数组MenuItem)我想用自定义模板重复menuitems:
export class App {
menuItems : MenuItem[];
}
export class MenuItem{
label:string;
}
Run Code Online (Sandbox Code Playgroud)
在我的应用模板中,我使用自定义元素
<require from="./menu-item"></require>
<ul>
<menu-item repeat.for="item of menuItems"></menu-item>
</ul>
Run Code Online (Sandbox Code Playgroud)
我的自定义模板(menu-item.html):
<template>
<li>${label}</li>
</template>
Run Code Online (Sandbox Code Playgroud)
获取模板绑定或访问绑定的MenuItem的正确方法是什么?
我已经试过如下:${label}和${item.label},但不起作用.我可以在bind(bindingContext)回调中看到bindingContext有一个属性'item':bindingContext.item这是被绑定的MenuItem.
我还尝试在MenuItem类上创建可绑定属性:
export class MenuItem{
@bindable current any;
label:string;
}
Run Code Online (Sandbox Code Playgroud)
和以下中继器:
<menu-item repeat.for="item of menuItems" current.bind="item"></menu-item>
Run Code Online (Sandbox Code Playgroud)
和相应的模板
<template>
<li>${current.label}</li>
</template>
Run Code Online (Sandbox Code Playgroud)
注意:请参阅下面的编辑1,以获取我对代码中此点的评论.
这种方法也行不通.
其他探索包括不使用自定义元素(工作),使用<compose view-model='MenuItem', model.bind='item'/>,在这个例子中也不起作用,我想也会详细说明.
工作解决方案,另请参阅Aurelia repeat.for绑定自定义元素 :
重复并绑定模板和viewmodel类的自定义属性:
<menu-item repeat.for="item of menuItems" current.bind="item" containerless></menu-item>
Run Code Online (Sandbox Code Playgroud)
viewmodel类:
import {bindable, customElement} …Run Code Online (Sandbox Code Playgroud) 我想用i18next格式化嵌套翻译
鉴于资源:
{"translation":{
"en":{
"food" : "bread",
"food_is_good" : "$t(food), that's not bad"
}
}
Run Code Online (Sandbox Code Playgroud)
和格式化功能:
function format(value, format, lng) {
if (value==undefined) return value;
switch (format) {
case 'capitalize': return _.capitalize(value);
default : return value;
}
}
Run Code Online (Sandbox Code Playgroud)
用于i18next的初始化:
...
interpolation: { format: format },
...
Run Code Online (Sandbox Code Playgroud)
我希望输出为“面包,那还不错”。所以我希望像这样:
{
...
"food_is_good_1" : "$t(food,capitalize), that's not bad",
"food_is_good_2" : "{{$t(food),capitalize}}, that's not bad",
"food_is_good_3" : "{{food,capitalize}}, that's not bad",
...
}
Run Code Online (Sandbox Code Playgroud)
会成功的 第一个选项显示错误:"failed parsing options string in nesting"
后两个选项警告:missed to …
要创建 Elastic Beanstalk 应用程序和环境,我有以下代码:
// this: the class instance extending Construct
const application = new CfnApplication(this, 'Application', {
applicationName: 'some-name'
});
const environment = new CfnEnvironment(this, 'Environment', {
environmentName: 'production',
applicationName: application.applicationName,
platformArn: 'arn::of::plaform',
solutionStackName: 'a-valid-stack-name'
});
Run Code Online (Sandbox Code Playgroud)
在 Route53 中创建别名记录需要一个目标实现IAliasRecordTarget
const record = new AliasRecord(this, 'ARecord', {
recordName: 'a-record',
target: ?
zone: zone
});
Run Code Online (Sandbox Code Playgroud)
如何使用环境作为目标?在 aws-cdk 存储库中寻找实现 IAliasRecordTarget 的类,除了 Cloudfront 分发和基本负载均衡器之外,不会产生很多候选对象