UA-17470720-3

Jump to content


Photo
- - - - -

Dynamic SCPropertyDefinition.dataType

data-definitions general models

  • Please log in to reply
5 replies to this topic

#1 Dima Zen

Dima Zen

    Starting Out

  • STV Lite
  • Pip
  • 7 posts
Reputation: 0
Neutral

Posted 06 March 2015 - 09:21 AM

Hello. I'm a newby to STV and looking for a interesting feature, that is core of my project, however can't find it. 

 

What I need is to construct data definition based on object property. My model represented as: 

@interface Note: NSObject

@property ... name; 
@property (nonatomic, strong) NSArray *fields; 

@end

 

While fields declared as: 

@interface Field: NSObject

@property (nonatomic, strong) id value;
@property (nonatomic, copy) NSString *type;

@end

 

Use case is next: app fetches set of Notes from the backend. Every Note has number of fields of the same class but different content. In order to describe Field we have type property, that can looks like "number", "text", "date" and so on. 

 

From samples I've understood, that it is possible to build DataDefinition with all the types I have, however it is kind of static. 

So my question: is it possible to evaluate type of every Field object in runtime and then build correct DataDefinition?

In the end I need to have a table with list of cells for every Field object and correct type (i.e. date for "date", "text" for "url" and so on).

Thanks in advance! 

 



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 March 2015 - 07:59 AM

Hi Dima,

 

is it possible to evaluate type of every Field object in runtime and then build correct DataDefinition?

 

Sure! But in that case you won't be creating a data definition, but rather creating the required cell directly at runtime. For instance, your code might look something like this:

 

// fieldObjectsSection is the SCArrayOfObjectsSection displaying the list of Field objects
    fieldObjectsSection.sectionActions.detailModelConfigured = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)
    {
        SCTableViewSection *detailSection = [detailModel sectionAtIndex:0];
        Field *currentFieldObject = (Field *)detailSection.boundObject;
        NSString *fieldType = currentFieldObject.type;
        
        if([fieldType isEqualToString:@"Text"])
        {
            SCTextFieldCell *textFieldCell = [SCTextFieldCell cellWithText:@"Enter Text" boundObject:currentFieldObject boundPropertyName:@"value"];
            [detailSection addCell:textFieldCell]; // you can also insert it anywhere you want in the section using insertCell:atIndex:
        }
        else
            if([fieldType isEqualToString:@"Date"])
            {
                SCDateCell *dateCell = [SCDateCell cellWithText:@"Select Date" boundObject:currentFieldObject boundPropertyName:@"value"];
                [detailSection addCell:dateCell];
            }
        //etc
    };

 

Hope this helps.



#3 Dima Zen

Dima Zen

    Starting Out

  • STV Lite
  • Pip
  • 7 posts
Reputation: 0
Neutral

Posted 10 March 2015 - 08:06 AM

Hi Dima,

 


 

Sure! But in that case you won't be creating a data definition, but rather creating the required cell directly at runtime. For instance, your code might look something like this:

 

// fieldObjectsSection is the SCArrayOfObjectsSection displaying the list of Field objects
    fieldObjectsSection.sectionActions.detailModelConfigured = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)
    {
        SCTableViewSection *detailSection = [detailModel sectionAtIndex:0];
        Field *currentFieldObject = (Field *)detailSection.boundObject;
        NSString *fieldType = currentFieldObject.type;
        
        if([fieldType isEqualToString:@"Text"])
        {
            SCTextFieldCell *textFieldCell = [SCTextFieldCell cellWithText:@"Enter Text" boundObject:currentFieldObject boundPropertyName:@"value"];
            [detailSection addCell:textFieldCell]; // you can also insert it anywhere you want
        }
        else
            if([fieldType isEqualToString:@"Date"])
            {
                SCDateCell *dateCell = [SCDateCell cellWithText:@"Select Date" boundObject:currentFieldObject boundPropertyName:@"value"];
                [detailSection addCell:dateCell];
            }
        //etc
    };

 

Hope this helps.

 

 

Hello, Tarek! 
Thanks for your reply! 

 

What if we'll go in another way:

When specific Note selected I'm manually creating data definition (sorry for misuse of terms) for every object in fields array separately? 

In other words at the moment of selection I already know schema of that Note, so in pseudocode it might looks like that:

 

noteDefinition = ... 

NSMutableArray *fieldDefinitions = ...
for (Field *field in note.fields) {
  if (field.type isEqualToString:@"Text") {
    ... textFieldDefinition = ..
    [fieldDefinitions addObject:textFieldDefinition]; 
  } else if ( and so on )

}

// at this point we have array of definition for every corresponding object in note.fields array
noteDefinition.setSomehowThisFieldDefinitions = fieldDefinitions

 

Sorry for lot of "...", just have no idea of what class should be used! 


Edited by Dima Zen, 10 March 2015 - 08:07 AM.


#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 March 2015 - 08:14 AM

No worries at all Dima :)

 

By definition (no pun intended), a "data definition" defines a structure for *all* of your specific class objects. So only one data definition is created per each class (or entity if you're using Core Data for instance), not per each object. I believe the cleanest approach would be to use the code I gave above.

 

Having said that, it also might be a good idea for us to create a new type of property definition type called "Dynamic", in which you supply the property definition type per object at runtime. If your project can wait, we can have this ready in about 2-3 weeks.



#5 Dima Zen

Dima Zen

    Starting Out

  • STV Lite
  • Pip
  • 7 posts
Reputation: 0
Neutral

Posted 10 March 2015 - 08:37 AM

Tarek, thanks for you help, really. 

 

It would be great to have dynamic property definition, however i'm not sure this is something i was talking about :) Technically what I need is to bound object at specific index of the relationship with correct property definition based on the value of "type" field. Well, since all my objects represented by a single class it seems, that solution provided by you is the only way out for now. 



#6 Dima Zen

Dima Zen

    Starting Out

  • STV Lite
  • Pip
  • 7 posts
Reputation: 0
Neutral

Posted 10 March 2015 - 08:38 AM

I'm also still in research, because it is possible for me to map Field into a concrete object subclasses as well. 







Also tagged with one or more of these keywords: data-definitions, general, models

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users