JavaScript
Now we will create users for our selections.
- var dogBreeds = [
- "affenpinscher"
- "african"
- "appenzeller"
- "beagle"
- "boxer"
- "chow"
- "chihuahua"
- "dingo"
];
- // We then add array that will add user selection//
- var userSelection = [];
Select and Remover USER
Lets now select a user that we want to view in our model and we will be able to remove a selected user. We can add and remove a selected dog breed in inside the "Dog Breed List". To deselect a breed, tap on it again.
-
- selectBreed:function(id)
- var breed = dogBreeds[id];
- var elementExists = document.getElementById(id);
- if(elementExists){
- //remove value from array
- elementExists.remove();
- var x = null;
- for (var i = 0; i < userSelection.length; i++) {
- if(userSelection[i] == breed){
- x = i;
- }
}
- userSelection.splice(x,1);
- }else{"
- var individualBreed = $('');
- $('#selection-list').append(individualBreed);
- "userSelection.push(dogBreeds[id]);
}
}"
Images
We will now be able to view an image of a selected user. In this section we linked a website that allow us to give images of the breeds : DogBreed This is a URL that has the images of the Dog Breeds.
- viewImages: function(){
- var dog = userSelection[userSelection.length-1];
- if(userSelection.length< 1) {
- dog = dogBreeds[0];
- }
- //GET URL
- var url = "https://dog.ceo/api/breed/";
- url += dog;
- url += "/images/random";
- url += "/images/random";
- url:url,
- type:"GET",
- beforeSend:function(){
- },
- success:function(data){
- console.log(data);
- // var item = JSON.parse(data);
- var url_image = data.message;
- $('#img_searched_image').attr('src',url_image);
- }
- })
- }
- };
Search Button
Here we will be impletmenting a serach function. This Search funtion will print out a select Dog Breed.
- $(function(){
- myViewModel.populateData();
- $('button').on('click', function(){
- myViewModel.viewImages();
- });
- })
Conclusion
I hope that after reading this, everyone learned something they didn't know before. I personally learned a number of things during the course of writing it! We now have a fully functioning Kockout.js view model.
If you have made it this far, I appreciate your time and diligence and I look forward to writing more fun tutorials here in the future.