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

[7_5_X][TIMOB-26499]: Remove namespace restriction in item templates #10410

Merged
merged 3 commits into from
Oct 31, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 40 additions & 40 deletions iphone/Classes/TiViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -2946,57 +2946,57 @@ - (void)unarchiveFromTemplate:(id)viewTemplate_
+ (TiViewProxy *)unarchiveFromTemplate:(id)viewTemplate_ inContext:(id<TiEvaluator>)context
{
TiViewTemplate *viewTemplate = [TiViewTemplate templateFromViewTemplate:viewTemplate_];
if (viewTemplate == nil) {
if (viewTemplate == nil || viewTemplate.type == nil) {
return nil;
}

if (viewTemplate.type != nil) {
TiViewProxy *proxy = nil; // A proxy can be constructed either

TiViewProxy *proxy = nil;
@try {
// First try a native proxy class...
if ([viewTemplate.type hasPrefix:@"Ti."] || [viewTemplate.type hasPrefix:@"Titanium."]) {
proxy = (TiViewProxy *)[[self class] createProxy:viewTemplate.type withProperties:nil inContext:context];
[context.krollContext invokeBlockOnThread:^{
[context registerProxy:proxy];
[proxy rememberSelf];
}];
proxy = (TiViewProxy *)[[self class] createProxy:viewTemplate.type withProperties:nil inContext:context];
[context.krollContext invokeBlockOnThread:^{
[context registerProxy:proxy];
[proxy rememberSelf];
}];
} @catch (NSException *exception) {
// No such class exists, so fall back to trying to load an Alloy widget or CommonJS module
// TODO Cache by name? Otherwise it seems to keep trying to load native class repeatedly (and failing) and has to re-eval this below!
// TODO eval once and pass in the controller name and props as args?
DebugLog(@"[DEBUG] Failed to load native class %@, trying Alloy widget / CommonJS module", viewTemplate.type);
NSString *code = [NSString stringWithFormat:@"var result;"
"try {"
" var jsModule = require('/alloy/widgets/%@/controllers/widget');"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but should this be either:

require('./alloy/widgets/%@/controllers/widget');
require('alloy/widgets/%@/controllers/widget');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should. The first is a relative URL, the second is a legacy-style path that we should eventually not support (as it was treated as either relative or absolute, and I think differed by platform as to which is tried first).

The alloy directory should be a to-level dir under the app's Resources, so the absolute path should be correct to load it.

" if (!jsModule) {"
" jsModule = require('%@');"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, require throws JS exception instead of returning null nor undefined (tested on iOS emulator). So I think requiring wrong Alloy widget never fallback to CommonJS module here.

" }"
" if (jsModule) {"
" result = function (parameters) {"
" const obj = new jsModule(parameters);"
" return obj.getView();"
" };"
" }"
"} catch (e) {"
" Ti.API.error('Failed to load Alloy widget / CommonJS module \"%@\" to be used as template');"
"}"
"result;",
viewTemplate.type, viewTemplate.type, viewTemplate.type];
id result = [context evalJSAndWait:code];
if (result != nil) {
KrollCallback *func = (KrollCallback *)result;
id contructResult = [func call:@[ viewTemplate.properties ] thisObject:nil]; // TODO: Do some error-handling here if the widget contructor fails?
proxy = (TiViewProxy *)contructResult;
} else {
// No such class exists, so fall back to trying to load an Alloy widget or CommonJS module
// TODO Cache by name? Otherwise it seems to keep trying to load native class repeatedly (and failing) and has to re-eval this below!
// TODO eval once and pass in the controller name and props as args?
NSString *code = [NSString stringWithFormat:@"var result;"
"try {"
" var jsModule = require('/alloy/widgets/%@/controllers/widget');"
" if (!jsModule) {"
" jsModule = require('%@');"
" }"
" if (jsModule) {"
" result = function (parameters) {"
" const obj = new jsModule(parameters);"
" return obj.getView();"
" };"
" }"
"} catch (e) {"
" Ti.API.error('Failed to load Alloy widget / CommonJS module \"%@\" to be used as template');"
"}"
"result;",
viewTemplate.type, viewTemplate.type, viewTemplate.type];
id result = [context evalJSAndWait:code];
if (result != nil) {
KrollCallback *func = (KrollCallback *)result;
id contructResult = [func call:@[ viewTemplate.properties ] thisObject:nil]; // TODO: Do some error-handling here if the widget contructor fails?
proxy = (TiViewProxy *)contructResult;
} else {
[self throwException:@"Invalid item template type provided"
subreason:@"The item template type provided cannot be resolved."
location:CODELOCATION];
}
[self throwException:@"Invalid item template type provided"
subreason:@"The item template type provided cannot be resolved."
location:CODELOCATION];
}
} @finally {
if (proxy != nil) {
[proxy unarchiveFromTemplate:viewTemplate];
return proxy;
}
}

return nil;
}

Expand Down