Building a Chat Web App With Signal R, Part 2: Listing 5

The KnockOut peopleViewModel.

 function peopleViewModel() {
    people(mappedPeople);    this.hub = $.connection.personHub;
}    this.people = ko.observableArray([]);
    this.newPersonFirstName = ko.observable();
    this.newPersonLastName = ko.observable();
    this.newPersonEmail = ko.observable();
    var people = this.people;
    var self = this;
    var notify = true;

    this.init = function () {
        this.hub.server.getAll();
    }

    this.hub.client.allPeopleRetrieved = function (allPeople) {
        var mappedPeople = $.map(allPeople, function (person) {
            return new personViewModel(person.Id, person.FirstName,
                person.LastName, person.Email, self)
        });

        people(mappedPeople);
    }

    this.hub.client.personUpdated = function (updatedPerson) {
        var person = ko.utils.arrayFilter(people(),
            function(value) {
                return value.id == updatedPerson.Id;
            })[0];
        notify = false;
        person.firstName(updatedPerson.FirstName);
        person.lastName(updatedPerson.LastName);
        person.email(updatedPerson.Email);
        notify = true;
    };

    this.hub.client.raiseError = function (error) {
        $("#error").text(error);
    }

    this.hub.client.personCreated = function (newPerson) {
        people.push(new personViewModel(newPerson.Id, newPerson.FirstName, newPerson.LastName,
            newPerson.Email, self));
    };

    this.hub.client.personRemoved = function (id) {
        var person = ko.utils.arrayFilter(people(), function(value) {
            return value.id == id;
        })[0];
        people.remove(person);
    }

    this.createPerson = function () {
        var person = { firstName: this.newPersonFirstName(), lastname: this.newPersonLastName(), email: this.newPersonEmail() };
        this.hub.server.add(person).done(function () {
            console.log('Person saved!');
        }).fail(function (error) {
            console.warn(error);
        });
        this.newPersonEmail('');
        this.newPersonFirstName('');
        this.newPersonLastName('');
    }
   
    this.deletePerson = function (id) {
        this.hub.server.delete(id);
    }

    this.updatePerson = function (person) {
        if (notify) {
            this.hub.server.update(person);
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

Upcoming Events