0

I have experience with WPF and MVVM with Caliburn.Micro and trying to implement a simple example in Angular2.

I had in WPF:

  1. View: completely separated from business logic, only contains code for the view
  2. ViewModel: it contains the business logic
  3. Commands: like services in Angular2

I want to implement this:

After clicking on the button, send the text from the TextBox to the server and clear the TextBox. Disable the button if the TextBox is empty.

I implemented this in WPF like this:

This is my View:

<TextBox Name="Message" /> <!-- Binding created by Caliburn.Micro -->
<Button Name="Save" /> <!-- Binding created by Caliburn.Micro -->

This is my ViewModel:

public string Message { get; set { // ... NotifyPropertyChanged ... } }

public bool CanSave 
{ 
    get 
    {
        return !String.IsNullOrEmpty(Message);
    }
}

public void Save
{
     var cmd = new SaveCommand(Message);
     cmd.Run();
     Message = null;
}

This is the command:

public class SaveCommand 
{ 
     public void Run()
     {
          // ...
     }
}

As you see I didn't had to write Click handlers, create binding etc. It was nicely handled by Caliburn. No clutter, it's just very simple and easily extendable. Save and CanSave is nicely separated too. I do not have to worry about CanSave in the Save method.

I could reuse the Command, reuse the ViewModel and even the View. I could create new ViewModels by inheriting from existing ViewModels. I could use the View with different ViewModels and vice versa.

How can I achieve similar functionality in Angular2? Should I put my business logic into a Component? The ViewModel is somehow missing from Angular2...?

Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
  • This might help you to get some idea https://coryrylan.com/blog/angular-form-builder-and-validation-management – Eldho Jun 08 '17 at 13:26
  • Thanks, but that's not what I am looking for. I know how to do this with a simple `Component`, but that's not enough :) – Dávid Molnár Jun 08 '17 at 14:07
  • @David, you really want to look at Aurelia then its very similar to CM since Rob created Caliburn, and some of the same principles are in place with http://aurelia.io/ – mvermef Jun 11 '17 at 06:07

0 Answers0