简体   繁体   English

我不想两次调用自定义函数

[英]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:

JS JS

inMyProjects: function (id) {
    var match = ko.utils.arrayFirst(vm.myProjects(), function (item) {
        return item.ProjectId() === id;
    });
    return match != null;
}

CSS: CSS:

projects li.in-my-projects {
    background-color: #EEE;
    border-color: #EEE;
}

projects li {
    background-color: #777;
    border-color: #777;
}

HTML: HTML:

<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.

相关问题 我想从函数中调用javascript函数,但是它不起作用 - I want to call javascript function from function, but it don't work 我的 function 有问题,我不想用现有坐标创建节点 - I have a problem about my function, I don't want to create a node with existing coordinates Angular 我不使用 (load) 调用函数 - Angular I don't call a function with (load) 为什么必须两次单击此输入按钮才能调用函数? - Why do I have to click this input button twice to call a function? 我想在 Node Js 中使用 Readline 从用户那里获取一些输入。 而且我不知道在哪里打电话给 function - I want's to take some input from user using Readline in Node Js. And I don't Know where to call the function 将参数传递给我不想立即执行的函数 - Passing argument to function I don't want to execute immediately 如果我没有页面特定的API调用怎么办? - what to do if I don't have page specific API call? 为什么我在调用函数时没有得到输入? - Why don't I get an input when I call the function? 使用AngularJS,我不想设置全局$ http.defaults.headers.common。 我可以在每次$ resource调用时发送自定义标头吗? - With AngularJS, I don't want to set the global $http.defaults.headers.common. Can I send my custom header with each $resource call? 如果我没有太多内容,如何在页面底部获得页脚,但我不希望粘贴页脚 - If I don't have much content, how to get the footer at the bottom of the page but I don't want a sticky footer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM