示例#1
0
        public SimpleExampleStore(AppDispatcher dispatcher)
        {
            if (dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            }

            _dispatcher = dispatcher;

            LastUpdated     = DateTime.Now;
            Message         = "Hi!";
            ValidationError = "";

            _dispatcher.Register(message =>
            {
                message
                .If <MessageChangeAction>(action =>
                {
                    Message         = action.Value;
                    ValidationError = (action.Value.Trim() == "") ? "Why no message??" : "";
                })
                .Else <TimePassedAction>(action => LastUpdated = DateTime.Now)
                .IfAnyMatched(OnChange);
            });
        }
        public SimpleExampleStore(HTMLElement renderContainer, AppDispatcher dispatcher)
        {
            if (renderContainer == null)
            {
                throw new ArgumentNullException("renderContainer");
            }
            if (dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            }

            _renderContainer = renderContainer;
            _dispatcher      = dispatcher;
            _viewModel       = new ViewModel(lastUpdated: DateTime.Now, message: "Hi!", validationError: "");
            RenderIfActive();

            _dispatcher.Register(message =>
            {
                var recordChange = message.Action as RecordChangeAction <ViewModel>;
                if (recordChange != null)
                {
                    UserEdit(recordChange.Value);
                    return;
                }

                if (message.Action is TimePassedAction)
                {
                    _viewModel = new ViewModel(lastUpdated: DateTime.Now, message: _viewModel.Message, validationError: _viewModel.ValidationError);
                    RenderIfActive();
                    return;
                }
            });
        }
示例#3
0
        public ToDoStore()
        {
            AppDispatcher.Register <CreateItemAction>(
                create =>
            {
                var newText = create.Message.Trim();
                if (string.IsNullOrWhiteSpace(newText))
                {
                    return;
                }

                Create(newText);
                text = "";
                EmitChange();
            });

            AppDispatcher.Register <CheckedItemAction>(
                action =>
            {
                if (items.ContainsKey(action.Id))
                {
                    var found = items[action.Id];
                    if (found.IsComplete)
                    {
                        System.Diagnostics.Debug.Fail("Item is not in a valid state for this action");
                    }
                    found.IsComplete = true;
                    EmitChange();
                }
            });

            AppDispatcher.Register <UncheckedItemAction>(
                action =>
            {
                if (items.ContainsKey(action.Id))
                {
                    var found = items[action.Id];
                    if (!found.IsComplete)
                    {
                        System.Diagnostics.Debug.Fail("Item is not in a valid state for this action");
                    }
                    found.IsComplete = false;
                    EmitChange();
                }
            });

            AppDispatcher.Register <EditItemAction>(
                action =>
            {
                if (items.ContainsKey(action.Id))
                {
                    var found = items[action.Id];
                    if (found.IsEditable)
                    {
                        System.Diagnostics.Debug.Fail("Item is not in a valid state for this action");
                    }
                    found.IsEditable = true;
                    EmitChange();
                }
            });

            AppDispatcher.Register <SaveItemAction>(
                action =>
            {
                if (items.ContainsKey(action.Id))
                {
                    var found = items[action.Id];
                    if (!found.IsEditable)
                    {
                        System.Diagnostics.Debug.Fail("Item is not in a valid state for this action");
                    }
                    found.Text       = action.Text;
                    found.IsEditable = false;
                    EmitChange();
                }
            });

            AppDispatcher.Register <ToggleAllCompletedAction>(
                action =>
            {
                foreach (var item in items)
                {
                    item.Value.IsComplete = action.IsCompleted;
                }
                EmitChange();
            });

            AppDispatcher.Register <ClearCompletedTasksAction>(
                action =>
            {
                foreach (var item in items.Where(x => x.Value.IsComplete).ToList())
                {
                    items.Remove(item.Key);
                }
                EmitChange();
            });
        }