0

I probably show off my lack of skills with this one. But since I´m all for learning I´m willing to let pride go...

In my viewModel I define:

self.Tags() = ko.observableArray();
self.Skills() = ko.observableArray();

then I want to save my tags. I have created this function outside the viewModel where I do some processing:

function saveTags( category, mytagsArray) { ... }

I call 'saveTags( "skill", Skills)' from within the viewModel and get this error telling me self.Tags() is not a function.

Why am I not allow to refer self.Tags inside the function "saveTags"?

(And please do not say it should be LackOfSkills instead of Skills... ;)

1
  • check my demo or the sample code I have pasted. It uses the object literal way of defining your viewmodel.
    – KKS
    Commented Oct 14, 2013 at 13:40

4 Answers 4

2

try without the ()'s:

self.Tags = ko.observableArray();
self.Skills = ko.observableArray();

self.Tags = foo says to set the self.Tags to be foo. self.Tags() says that self.Tags in a function and you want to invoke it. Those are both legit statements. But self.Tags() = foo says self.Tags is a function you want to invoke and set it's value to foo... which doesn't make sense and isn't valid code in any language.

1

Here is a demo pen for you. Try defining your view model like this:

var tagsViewModel = {
    // data
    tagToAdd: ko.observable(""), // this is the new tag to add
    tags: ko.observableArray([]), //empty collection
    skills: ko.observableArray([]),

// behaviors
    addTag: function () {
        var newTag = { Name: this.tagToAdd() };
        this.tagToAdd("");

        tagsViewModel.tags.push(newTag)

    }
};

ko.applyBindings(tagsViewModel);

Define your html like this:

<input type="text" placeholder="Add New Tag" data-bind="value: tagToAdd, valueUpdate: 'afterkeydown'" /> <button data-bind="click: addTag, enable: tagToAdd().length > 0" class="btn"><i class="icon-plus"></i> Add</button>

 <ul data-bind="foreach: tags" class="">
<!-- DEFINE UR LI HERE -->
</ul>
3
  • 1
    @AsleG check my demo or the sample code I have pasted. It uses the object literal way of defining your viewmodel.
    – KKS
    Commented Oct 14, 2013 at 13:38
  • 1
    @AsleG As you can see in the addtag function, I am using tags, tagtoadd properties, in the same way you can use from your SaveTags function. Only the name is different but it is doing the same thing I guess i.e. adding item to array.
    – KKS
    Commented Oct 14, 2013 at 13:56
  • @AsleG if my sample demo or the above code solves your problem or informs you to how to write your vm in a right way, kindly accept my soln as answer :P
    – KKS
    Commented Oct 14, 2013 at 14:02
1

You can access your viewmodel by storing it in a variable like so:

function ViewModel() {
     self.Tags = ko.observableArray();
     self.Skills = ko.observableArray();
}
my = { viewModel: new ViewModel() };
ko.applyBindings(my.viewModel);

Then you just access my.Tags()

related answer

3
  • 1
    i just copy pasted his code without checkin. thank you it's fixed now Commented Oct 14, 2013 at 13:21
  • Thanks @Circadian! This is pretty close to my model. Inside the function ViewModel() {} I have a method self.saveAll = function(){...} It is from within this I wanted to call "saveTags()" as an external function. (I can get it to work if I include "self.SaveTags()" as an ViewModel-internal method as well. So would I be able to do that with refering to my.Tasks() etc?
    – Asle G
    Commented Oct 14, 2013 at 13:35
  • i guess so yes. try it out Commented Oct 14, 2013 at 14:07
1

The problem here is that you assigning ko.observableArray(); to result of the call self.Tags(), and that is why your function is not stored in self.Tags variable. I guess the right code will look like:
in case you want assign the results of that calls:

self.Tags = ko.observableArray();
self.Skills = ko.observableArray();

in case you want assign the function links to call them later:

self.Tags = ko.observableArray;
self.Skills = ko.observableArray;
...
// somewhere later
self.Tags();

Not the answer you're looking for? Browse other questions tagged or ask your own question.