最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if a knockout observable exists? - Stack Overflow

programmeradmin0浏览0评论

Say I have a json of observables :

self.name = ko.observable('Arnab');
self.surname= ko.observale('Bhagabati')

var person = {
  name:ko.observable('Arnab'),
  lastName= ko.observale('Bhagabati')
}

I can access the name of person by console.log(person.name());

How do I check whether my person has a knockout observable property called, say salary?

if (typeof person.salary() !== 'undefined')

gives params.salary is not a function

Say I have a json of observables :

self.name = ko.observable('Arnab');
self.surname= ko.observale('Bhagabati')

var person = {
  name:ko.observable('Arnab'),
  lastName= ko.observale('Bhagabati')
}

I can access the name of person by console.log(person.name());

How do I check whether my person has a knockout observable property called, say salary?

if (typeof person.salary() !== 'undefined')

gives params.salary is not a function

Share Improve this question asked Nov 7, 2017 at 11:57 Arnab BhagabatiArnab Bhagabati 2,7444 gold badges36 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

How do I check whether my person has a knockout observable property called, say salary?

(emphasis mine)

If you use a check along the lines of if (ko.unwrap(person.salary)), like the other answers suggest, you're basically checking if there's a defined salary property.

I.e.:

person = { salary: undefined };
person = { };
person = { salary: ko.observable() };

Will all return true for typeof ko.unwrap(person.salary) === "undefined"

If I take what you wrote literally, this doesn't answer whether the 'person' object has an observable property called salary.

To answer this question, you'll need ko.isObservable:

var persons = [
  [{ }, "{ }"],
  [{ salary: undefined }, "{ salary: undefined }" ],
  [{ salary: ko.observable() }, "{ salary: ko.observable()" ]
]

var check1 = person => typeof ko.unwrap(person.salary) === "undefined";
var check2 = person => ko.isObservable(person.salary);

console.log(check1.toString());
persons.forEach(([p, c]) => console.log(c, "->", check1(p)));

console.log(check2.toString());
persons.forEach(([p, c]) => console.log(c, "->", check2(p)));
<script src="https://cdnjs.cloudflare./ajax/libs/knockout/3.4.2/knockout-min.js"></script>

It might be that I took your question too literally, and all you need is ko.unwrap, but I felt I had to share the difference

observables are functions. If there is no salary property, person.salary() will throw an exception.

So you can use:

if (typeof ko.unwrap(person.salary) !== 'undefined')
{

}

unwrap returns the value of the property, irrespective of whether it's observable or not.

Refer to this question on more info

var val = ko.unwrap(person.salary);

and

console.log(ko.unwrap(person.salary));

will work no matter whether person.salary is observable or not.

In the following code, != checks for both null and undefined:

if (person.salary != null) {
    // ...
}

If you want a quick solution, just see if the object type is a function, and then add a couple of checks for knockout specific functions to exist in it. This is handy to use in ko.puted, when you are pretty dern sure that the observable are GOING to be there, but aren't yet.

var isKoObservable = function(obs) {
  var retVal = false;
  if (typeof obs === "function") {
    if (typeof obs.subscribe === "function" && typeof obs.notifySubscribers === "function") {
      // its HIGHLY likely to be an observable or observableArray
      retVal = true; 
    }
  }

  return retVal;
}

See this SO answer for details: how to tell if a javascript variable is a function

发布评论

评论列表(0)

  1. 暂无评论