如何使用ko属性避免两次运行代码 [英] how to avoid run code twice using ko property

查看:123
本文介绍了如何使用ko属性避免两次运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个列表页面,其中有两个日期选择器和一个表.该页面的想法是使用日期选择器的默认参数为页面加载一些注册数据,此后,用户将能够根据日期选择器过滤器选择他要查看的内容.为了运行我的应用程序,我创建了两个属性fromDate和toDate,并且我使用suscribe方法来了解何时更改日期,以便在更改日期时我应该调用我的服务(或一些过滤代码).我的页面问题是,当前要加载页面时,默认情况下是根据我自己的逻辑设置控件的,因此应该两次调用应调用我的服务的代码(在loadInitData中调用一次,而在预订中调用其他调用)

I am creating a list page where there are two datepicker and one table. The idea of the page it's load the page with some registration data using default parameters for the datepickers, after that the user will be able to choose what he want to see according to the datepicker filters. To run my app I have created two properties fromDate and toDate and i am using suscribe method in order to know when the dates are changed, so that when the dates are changed i should call to my services (or some filtering code). My issue with my page is that at the moment to load my page the control are seted by default from my own logic, so that my code that should call to my service is called twice (one in loadInitData and others in the suscriptions)

define(["require", "exports", 'services/logger',  '../../services/Assessment/datacontext'], function(require, exports, __logger__, __datacontext__) {
var logger = __logger__;

var datacontext = __datacontext__;    
exports.title = 'AssessmentListing';   
exports.fromDate = ko.observable(new Date('2012/12/12'));
exports.toDate = ko.observable(new Date('2012/12/12'));

function activate() {
   loadInitData();
}

exports.activate = activate;
function loadInitData() { 
   var focusDate = ko.observable(new Date('2013/07/06'));
   exports.fromDate(firstDayOfMonth(focusDate));
   exports.toDate(getLastDayOfMonth(focusDate));
   // calls to services
} 

exports.toDate.subscribe(function (newValue) {
   /*THIS CODE SHOULD BE EXECUTED JUST WHEN THE USER CHANGE THE DATE FROM THE CONTROL*/
   alert("new selection :");
});
exports.fromDate.subscribe(function (newValue) {
   /*THIS CODE SHOULD BE EXECUTED JUST WHEN THE USER CHANGE THE DATE FROM THE CONTROL*/
   alert("new selection");
});

function getLastDayOfMonth(focusDate) {
  var d = new Date(Date.apply(null, focusDate));
  d.setMonth(d.getMonth() + 1);
  d.setDate(0);
  return d;
}

function firstDayOfMonth(focusDate) {
   var d = new Date(Date.apply(null, arguments));
   d.setDate(1);
   return d;
}

function viewAttached() {
}
exports.viewAttached = viewAttached;
})

请尝试在loadInitData的开头和loading(false)以及loadInitData的末尾添加一个新的布尔属性,以使loading(true)生效.这项工作很好.但仍会多次调用该订阅代码.

Try add a new boolean property to say loading(true) at the begining of loadInitData and loading(false) and the end of loadInitData. This work's fine. but still the suscription code is called more than one time.

推荐答案

解决此问题的一种简单方法是延迟设置订阅,直到初始化视图模型为止.

Probably a simple way to solve this is to delay setting up the subscriptions until your view model is initialized:

function activate() {
   loadInitData();

    exports.toDate.subscribe(function (newValue) {
       /*THIS CODE SHOULD BE EXECUTED JUST WHEN THE USER CHANGE THE DATE FROM THE CONTROL*/
       alert("new selection :");
    });
    exports.fromDate.subscribe(function (newValue) {
       /*THIS CODE SHOULD BE EXECUTED JUST WHEN THE USER CHANGE THE DATE FROM THE CONTROL*/
       alert("new selection");
    });
}

这篇关于如何使用ko属性避免两次运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆