[英]I don't want to have to call custom function twice
I'm creating a <li></li>
list where I loop through a list of projects , and if the project exist in myProjects I highlight that background. 我正在创建一个
<li></li>
列表,在其中循环浏览项目列表,如果该项目存在于myProjects中, 则将突出显示该背景。
I need to use this function twice, once for the background property and once for the border property. 我需要使用此功能两次,一次用于背景属性,一次用于border属性。 But to me it seems redundant having to run it twice, so is there a way to kind of return the object to the view and use it twice there?
但是对我来说似乎必须重复运行两次,所以有没有办法将对象返回到视图并在那里使用两次? Or is there some other way around this?
还是有其他解决方法?
Maybe this won't affect the loading time that much anyway? 也许这不会对加载时间产生太大影响?
Model 模型
var vm = {
myProjects: ko.observableArray(),
projects: ko.observableArray(),
inMyProjects: function (id) {
var match = ko.utils.arrayFirst(vm.myProjects(), function (item) {
return item.ProjectId() === id;
});
return match != null ? '#EEE' : '#777';
}
};
View 视图
<ul class="projects" data-bind="foreach: projects">
<li data-bind="text: Description, style: {
backgroundColor: $parent.inMyProjects(ProjectId()),
borderColor: $parent.inMyProjects(ProjectId())
}">
</li>
</ul>
Make inMyProjects
return an object instead of the value and reuse the value in it: 使
inMyProjects
返回一个对象而不是该值,然后在其中重用该值:
var vm = { myProjects: ko.observableArray(), projects: ko.observableArray(), inMyProjects: function (id) { var match = ko.utils.arrayFirst(vm.myProjects(), function (item) { return item.ProjectId() === id; }); var vm = {myProjects:ko.observableArray(),项目:ko.observableArray(),inMyProjects:function(id){var match = ko.utils.arrayFirst(vm.myProjects(),function(item){返回项目。 ProjectId()=== id;}); var result = match != null ?
var result = match!= null吗? '#EEE' : '#777';
'#EEE':'#777';
return {
backgroundColor: result,
borderColor: result
};
}
};
And then use it once in the view: 然后在视图中使用一次:
<ul class="projects" data-bind="foreach: projects">
<li data-bind="text: Description, style: $parent.inMyProjects(ProjectId())">
</li>
</ul>
Sorry, a bit in a hurry so there may be a typo but you get the point. 抱歉,有点着急,所以可能会有错别字,但您明白了。
Don't rely on inline styling for things like this, introduce some CSS instead: 不要像这样使用内联样式,而是引入一些CSS:
.inMyProject {
background-color: #...;
border-color: #...;
}
You can then give your li
element this class with Knockout's CSS binding : 然后,您可以使用Knockout的CSS绑定为
li
元素提供此类:
<li data-bind="css: { 'inMyProject': $parent.inMyProjects(ProjectId()) }">
Use the css
binding with a class for both the background color and the border. 将
css
绑定与类一起用于背景色和边框。 inMyProject
will be called only once. inMyProject
将仅被调用一次。
Of course inMyProject
should return a boolan now: 当然
inMyProject
现在应该返回boolan:
inMyProjects: function (id) {
var match = ko.utils.arrayFirst(vm.myProjects(), function (item) {
return item.ProjectId() === id;
});
return match != null;
}
projects li.in-my-projects {
background-color: #EEE;
border-color: #EEE;
}
projects li {
background-color: #777;
border-color: #777;
}
<ul class="projects" data-bind="foreach: projects">
<li data-bind="text: Description,
css: { 'in-my-projects': $parent.inMyProjects(ProjectId()) }
}">
</li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.