question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi, Basarat, great work as usual 😉 Already using it in my project. I have one question though about array. How do you specify a field that contains an array of complex objects. In your docs you have two form states, is that necessary?

TL;DR;

So, can a FieldState contain an array of FormStates as its value?

Example:

class A {
  a: number;
  b: string;
}

class B = {
  arr: A[]
}

can I model it like this?

// i like to have all types globally available in order to reduce imports, don't judge ;)
declare global {
  namespace App.Models {
    type AStateFields = {
      a: FieldState<number>;
      b:  FieldState<string>;
    }

    type BStateFields = {
      arr: FieldState<AStateModel[]>;
    }

    type AState = AStateModel;
    type BState = BStateModel;
  }
}

class AStateModel extends FormState<App.Models.AStateFields> {
  constructor(a: A) {
    super({
      a: new FieldState(a.a),
      b: new FieldState(a.b),
    })
  }
}

class BStateModel extends FormState<App.Models.BStateFields> {
  constructor(b: B) {
    super({
      arr: new FieldState(b.map(item => new AStateModel(item)))
    })
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
tomitrescakcommented, Jan 19, 2017

G’day mate 😃! I saw your tweet and checked out the tutorial and was very happy with it! Thanks for the amazing work you are doing. Your effort is cutting down the development and refactoring time immensely and is just pleasure to work with it.

0reactions
basaratcommented, Jan 19, 2017

@tomitrescak So the way we’ve been handling this is by maintaining additional validation rules regarding the overall form state in our State classes.

But to make it easier to just use formstate just pushed a new version 0.10.0 that allows you to add validators to FormState. This works nice with your example. Also you don’t need to make arrays observable since FormState.$ is automatically made observable for you. Simplified your Cars / Features example further and used it as a tutorial here : https://formstate.github.io/demos/#formstate Hope you like it 🌹 ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Array? - GeeksforGeeks
An array is a collection of items of same data type stored at contiguous memory locations. This makes it easier to calculate the...
Read more >
Arrays (Java Platform SE 7 ) - Oracle Help Center
Searches a range of the specified array for the specified object using the binary search algorithm. static <T> int, binarySearch(T[] a, T key,...
Read more >
Array - JavaScript - MDN Web Docs
The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name ...
Read more >
Array - Wikipedia
An array is a systematic arrangement of similar objects, usually in rows and columns. The little push-buttons on the upper part of the...
Read more >
Java Arrays - W3Schools
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found