Click here to Skip to main content
15,885,366 members
Articles / Web Development / HTML5

Remotely databind a Collection with Spike & Knockout.js

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
23 Jun 2015CPOL3 min read 23K   312   26  
Making an ObservableCollection observable by remote javascript clients.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

namespace MyList
{
    /// <summary>
    /// Represents an event send remotely.
    /// </summary>
    public struct SyncListEvent
    {
        /// <summary>
        /// Constructs a new instance of <see cref="SyncListEvent"/>.
        /// </summary>
        /// <param name="args">Event arguments to clone.</param>
        public SyncListEvent(NotifyCollectionChangedEventArgs args)
        {
            this.Action = args.Action;
            this.NewItems = args.NewItems;
            this.NewIndex = args.NewStartingIndex;
            this.OldItems = args.OldItems;
            this.OldIndex = args.OldStartingIndex;
        }

        /// <summary>
        /// Constructs a new instance of <see cref="SyncListEvent"/>.
        /// </summary>
        /// <param name="items">A list of items to forward.</param>
        public SyncListEvent(IList items)
        {
            this.Action = NotifyCollectionChangedAction.Reset;
            this.NewItems = items;
            this.NewIndex = 0;
            this.OldItems = null;
            this.OldIndex = -1;
        }

        /// <summary>
        ///  Gets the action that caused the event.
        /// </summary>
        public readonly NotifyCollectionChangedAction Action;

        /// <summary>
        /// Gets the list of new items involved in the change.
        /// </summary>
        public readonly IList NewItems;

        /// <summary>
        /// Gets the index at which the change occurred.
        /// </summary>
        public readonly int NewIndex;

        /// <summary>
        /// Gets the list of items affected by a Replace, Remove, or Move action.
        /// </summary>
        public readonly IList OldItems;

        /// <summary>
        /// Gets the index at which a Move, Remove, or Replace action occurred.
        /// </summary>
        public readonly int OldIndex;

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants, Ph.D. is the architect behind emitter.io service, a real-time, low-latency publish/subscribe service for IoT, Gaming. He is a software engineer and scientist with extensive experience in different computer science domains, programming languages/principles/patterns & frameworks.

His main expertise consists of C# and .NET platform, game technologies, cloud, human-computer interaction, big data and artificial intelligence. He has an extensive programming knowledge and R&D expertise.



Comments and Discussions