Skip to content

Commit

Permalink
Bugfix for when $jason.head.data exists => Should wait for the data…
Browse files Browse the repository at this point in the history
… to render before calling onLoad()
  • Loading branch information
gliechtenstein committed Jan 1, 2017
1 parent 20b8f15 commit 75286ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/jasonette/seed/Core/JasonParser.java
Expand Up @@ -53,10 +53,11 @@ public void parse(final String data_type, final JSONObject data, final Object te
try{
new Thread(new Runnable(){
@Override public void run() {
try {

try {
// thread handling - acquire handle
juice.getLocker().acquire();

V8Object parser = juice.getObject("parser");

String templateJson = template.toString();
Expand All @@ -79,15 +80,16 @@ public void parse(final String data_type, final JSONObject data, final Object te
}
parser.release();

// thread handling - release handle
juice.getLocker().release();

res = new JSONObject(val);
listener.onFinished(res);

} catch (Exception e){
Log.d("Error", e.toString());
}

// thread handling - release handle
juice.getLocker().release();
}
}).start();
} catch (Exception e){
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
Expand Up @@ -60,6 +60,7 @@ public class JasonViewActivity extends AppCompatActivity{


private boolean firstResume = true;
private boolean loaded;

private int header_height;
private ImageView logoView;
Expand Down Expand Up @@ -90,6 +91,8 @@ protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

loaded = false;

// Initialize Parser instance
JasonParser.getInstance(this);

Expand Down Expand Up @@ -275,6 +278,7 @@ protected void onSaveInstanceState(Bundle savedInstanceState) {
*************************************************************/
void onShow(){
loaded = true;
try {
JSONObject head = model.jason.getJSONObject("$jason").getJSONObject("head");
JSONObject events = head.getJSONObject("actions");
Expand All @@ -286,6 +290,7 @@ void onShow(){
}
}
void onLoad(){
loaded = true;
trigger("$load", new JSONObject(), this);
onShow();
}
Expand Down Expand Up @@ -582,11 +587,19 @@ public void render(final JSONObject action, JSONObject data, Context context){
JasonParser.getInstance(this).setParserListener(new JasonParser.JasonParserListener() {
@Override
public void onFinished(JSONObject body) {
// in case we had $jason.head.data, need to trigger onLoad here
// instead of inside build()
// since on Load() gets triggered after everything has loaded
// In this case, model.rendered will be null here since it hasn't been rendered yet.
if(!loaded){
onLoad();
}

setup_body(body);
}
});

JasonParser.getInstance(this).parse(type, data, template, getApplicationContext());
JasonParser.getInstance(this).parse(type, data, template, context);

} catch (Exception e){
Log.d("Error", e.toString());
Expand Down Expand Up @@ -717,6 +730,10 @@ public void build(JSONObject options){
if (head.getJSONObject("templates").has("body")) {
model.set("state", new JSONObject());
render(new JSONObject(), model.state, this);

// return here so onLoad() below will NOT betriggered.
// onLoad() will be triggered after render has finished
return;
}
}
}
Expand Down

0 comments on commit 75286ce

Please sign in to comment.