Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to Model class another save method to return the response of the object being added #115

Open
serjooo opened this issue Mar 4, 2017 · 1 comment

Comments

@serjooo
Copy link

serjooo commented Mar 4, 2017

The way currently the save in Model is implemented is:

public void save(final VoidCallback callback) {
        invokeMethod(id == null ? "create" : "save", toMap(),
                new Adapter.JsonObjectCallback() {

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }

            @Override
            public void onSuccess(JSONObject response) {
                Object id = response.opt("id");
                if (id != null) {
                    setId(id);
                }
                callback.onSuccess();
            }
        });
    }

Sometimes after saving something to the database you care about getting the JsonResponseObject, like maybe getting the ID for the object saved from the ResponseObject. So I was thinking that we have another save method in the Model class that actually returns a JSONResponseObject like this

public void save(final JsonObjectCallback callback) {
        invokeMethod(id == null ? "create" : "save", toMap(),
                new Adapter.JsonObjectCallback() {

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }

            @Override
            public void onSuccess(JSONObject response) {
                Object id = response.opt("id");
                if (id != null) {
                    setId(id);
                }
                callback.onSuccess(response);
            }
        });
    }
@serjooo
Copy link
Author

serjooo commented Apr 9, 2017

I found a work around to this. What you can do is in your repository implement a method and make it accept an ObjectCallback and then when calling save you would actually create a new VoidCallback and implement its methods and then you would be able to call your ObjectCallback appropriately according to the VoidCallback and return the object. Here is an example to clarify what has been added:

public void createObject(String firstName, String lastName final ObjectCallback objectCallback) {
        HashMap<String, Object> params = new HashMap<>();

        params.put("firstName", firstName);
        params.put("lastName", lastName);

        final Object theObject = repository.createObject(params);
        theObject .setFirstName(employeeId);
        theObject .setLastName(valetTeamId);

        theObject.save(new VoidCallback() {
            @Override
            public void onSuccess() {
                objectCallback.onSuccess(theObject);
            }

            @Override
            public void onError(Throwable t) {
                objectCallback.onError(t);
            }
        });

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants