Skip to content

Examples

Kjell Nilsson edited this page Oct 9, 2018 · 3 revisions

Examples

Code standard

The best way is to always use singular names for entities Like Person or Customer. A to one relation to a person should be called person. A to many relation to a list of persons should be called persons You could call them something else but it will be more streamlined to do this.

If the columnName differ from the attributeName it is possible to add the actual column name in the user info. But you also have to add information about the foreign key etc in the user info dictionary on the relationship attribute in the model file.

How to fetch all rows of an entity

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How to add an operator to the fetch

An operator is extra information that is sent to the backend. It is used for example to tell the backend to do a lazy fetch (just send the primary keys) or count the number of objects (just send a number with the number of rows). The default behavior that is used with the objc-backend is to add the operator after the method in the url. Normal url for a fetch is http://localhost/backend/fetch/Person. If a lazy operator is added the url will look like http://localhost/backend/fetch/lazy/Person

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [fetchSpecification setOperator:@"lazy"];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How to use a special method (function) in the backend to fetch objects

The default behavior that is used with the objc-backend is to use the method fetch to fetch objects. Normal url for a fetch is http://localhost/backend/fetch/Person. If a special method is implemented in the backend to send objects to the client the url will look like http://localhost/backend/MyMethod/Person. This is how to tell the LOF to use an alternative method to fetch the objects

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];
    [fetchSpecification setMethod:@"MyMethod"];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];

How I can alter the request before it is sent to the backend.

    var fetchSpecification = [LOFetchSpecification fetchSpecificationForEntityNamed:@"Person" qualifier:nil];

    [fetchSpecification setRequestPreProcessBlock:function(request) {
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[CPString JSONFromObject:{
            sessionKey: theSessionKey,
            itemKeys: itemKeys
        }]];
    }];

    [objectContext requestObjectsWithFetchSpecification:fetchSpecification withCompletionHandler:function(objects, statusCode) {
        if (statusCode === 200) {
            // Do something with the objects
        }
    }];