如何使用基因剔除获得可观测值

时间:2018-07-27 10:07:41

标签: javascript knockout.js

我试图从定义视图模型的另一个脚本访问可观察值,但是我不知道该怎么做。让我解释一下这件事:

我的视图模型在monitoringviewmodel.js中定义,并在MonitoringTask.cshtml中使用,其中显示了从中传递的数据。在此网页中,我有一个选择列表,用户可以在其中选择他们要进入的页面。我的想法是将链接存储在每个value的{​​{1}}参数中。然后,我将获得用户选择的链接,并使用其值将其重定向到正确的页面。为了获取正确的数据,我决定在option上放置一个id以便进行简单识别(我的数据使用select显示,因此我不能只选择foreach标签,因为它在页面中多次出现。 select被置于id的可观察对象中。我的问题是我不知道如何在网页脚本中获取此可观察值的值。

如果您有任何想法...

谢谢

2 个答案:

答案 0 :(得分:0)

如果您有ID:

var element = document.getElementById(id);
var viewModel = ko.dataFor(element);
var value = viewModel.observable();

答案 1 :(得分:-1)

@Rajesh:我的视图模型的定义与网页分开。我想从我的网页访问可观察值,这就是为什么我不能只在网页脚本中使用override fun onBackPressed() { val currentDestination=NavHostFragment.findNavController(nav_host_fragment).currentDestination when(currentDestination.id) { R.id.loginFragment -> { finish() } } super.onBackPressed() }

@frooook:因为我使用observableVariable()绑定,并且我的ID是在foreach文件的viewmodel中生成的,所以我没有该元素的ID。

如果不清楚(并且bc @samuellawrentz让我把它放^^),这是我使用的代码(它需要库monitoringviewmodel.js和其他文件才能正常工作,因此不会按原样运行,但这是处理数据的主要部分,其余未包含的部分效果很好):

require.js

monitoringviewmodel.js

define([ 'common' ], function () { 'use strict'; //1000 ms = 1 second var callSmtpCheckTmp = 3600000; // chaque 1 heure var callBdCheckTmp = 600000; // chaque 10 min var checkServiceTmp = 60000; // chaque 1 min var checkFirstSmtpTmp = 60000; // chaque minute var checkRunningTmp = 10000; //chaque seconde var checkStatusTmp = 60000;// chaque minute var ko = require('knockout'); var ViewModel = function (message) { var _this = this; _this.listService = ko.observableArray( ko.utils.arrayMap(message.ListServiceModel, function (elem) { return new ServiceViewModel(elem); })); var sizeColumns = ComputeSizeColumns(); ko.utils.arrayForEach(_this.listService(), function(elem) { elem.sizeColumns = sizeColumns; }) CheckBdData(); // All the functions below work perfectly fine and are not releveant to the topic function CheckBdData() { ... } function ComputeSizeColumns() { ... } $(window).load(function () { setInterval(CheckBdData, callBdCheckTmp); }); }; var ServiceViewModel = function (items) { var _this = this; var checkFirstSmtp = true; _this.id = ko.observable(items.Id); _this.serviceName = ko.observable(items.ServiceName); _this.description = ko.observable(items.Description); _this.serviceUrl = ko.observable(items.ServiceUrl); _this.status = ko.observable(false); _this.checkSmtp = ko.observable(true); _this.listTask = ko.observableArray( ko.utils.arrayMap(items.ListTaskModel, function (elem) { return new TaskViewModel(elem, items.ServiceUrl, items.Id, _this.checkSmtp); })); _this.profitStatus = ko.computed(function () { var css = ''; if (_this.status() === true) { // css = "statusOk"; css = "glyphicon-ok"; } else { // css = "statusKO"; css = "glyphicon-remove"; } return css; }); _this.sizeColumns = ko.observable(""); checkStatus(); // All the functions below work perfectly fine and are not releveant to the topic function checkStatus() { ... } function CheckSMTPTask() { ... } function CheckFirstSMTPTask() { ... } $(window).load(function () { setInterval(checkStatus, checkServiceTmp); setInterval(CheckSMTPTask, callSmtpCheckTmp); setInterval(CheckFirstSMTPTask, checkFirstSmtpTmp); }); }; var TaskViewModel = function (items, serviceUrl, serviceId, checkSmtp) { var _this = this; var urlTask = serviceUrl + items.DisplayName + "/"; var errorLinkStr = new Link("Erreurs", "/Monitoring/ViewError?environnementId=" + environnementId + "&serviceId=" + serviceId + "&taskId=" + items.Id); var histLinkStr = new Link("Historique", "/Monitoring/ViewHistory?environnementId=" + environnementId + "&serviceId=" + serviceId + "&taskId=" + items.Id); var configLinkStr = new Link("Config", urlTask + "config"); var statusLinkStr = new Link("État", urlTask + "status"); var IdPopoverStr = 'Popover' + items.DisplayName; _this.id = ko.observable(items.Id); _this.serviceId = ko.observable(serviceId); _this.displayName = ko.observable(items.DisplayName); _this.bdName = ko.observable(items.BdName); _this.serviceUrl = ko.observable(serviceUrl + items.DisplayName + "/"); _this.description = ko.observable(items.Description); _this.status = ko.observable(''); _this.nextExecution = ko.observable(items.NextOccurrenceToString); _this.nbExec = ko.observable(items.NbExec); _this.nbError = ko.observable(items.NbError); _this.serviceUrl = ko.observable(items.ServiceUrl); _this.lastExec = ko.observable(items.LastExec); _this.execTaskLink = ko.observable(urlTask + "exec"); _this.errorLink = ko.observable(errorLinkStr.link); _this.histLink = ko.observable(histLinkStr.link); _this.configLink = ko.observable(configLinkStr.link); _this.statusLink = ko.observable(statusLinkStr.link); _this.runningLink = ko.observable(urlTask + "isrunning"); _this.isrunning = ko.observable(false); _this.checkBdd = ko.observable(false); _this.arrayLinks = ko.observableArray([errorLinkStr, histLinkStr, configLinkStr, statusLinkStr]); _this.Selectedlink = ko.observable(); _this.IdPopover = IdPopoverStr; _this.IdPopoverCall = "#" + IdPopoverStr; _this.PopoverContent = ko.observable("Tâche : " + items.DisplayName+ "\nDescription : " + items.Description); _this.profitStatus = ko.computed(function () { var css = ''; if (_this.status() === true) { //css = "statusOk"; css = "success"; } else { //css = "statusKO"; css = "danger"; } return css; }); checkStatus(); // All the functions below work perfectly fine and are not releveant to the topic function checkStatus() { ... } function RunningTask() { ... } $(window).load(function () { setInterval(checkStatus, checkStatusTmp); setInterval(RunningTask, checkRunningTmp); }); CheckBddTask(_this); }; var Link = function(name, link){ this.name = name; this.link = link; } // All the functions below work perfectly fine and are not releveant to the topic function CheckStatusTask(task, checkSmtp) { ... } function CheckRunningTask(task) { ... } function CheckStatusService(service) { ... } function CheckSMTPTaskService(service) { ... } function CheckBddTask(task) { ... } function GetBdInfo(listTask) { ... } return ViewModel; });

MonitoringTask.cshtml