ti-expert
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTitanium expert
Titanium 专家
Practical architecture and implementation guidance for Titanium SDK apps (Alloy and Classic). Focus on maintainability, clear boundaries, and low-friction testing.
为Titanium SDK应用(Alloy和Classic模式)提供实用的架构与实现指导,重点关注可维护性、清晰的边界划分以及低门槛测试。
Project detection
项目检测
:::info Auto-detects Alloy vs Classic projects
This skill detects project type automatically and tailors guidance.
Alloy indicators:
- folder (MVC structure)
app/ - ,
app/views/foldersapp/controllers/ - or
alloy.jmkfilesconfig.json
Classic indicators:
- folder with
Resources/at rootapp.js - No folder structure
app/
Behavior:
- Alloy detected: provides Alloy MVC patterns and Backbone.js guidance
- Classic detected: avoids Alloy-only patterns and recommends Classic options or migration
- Unknown: asks the user to clarify the project type :::
:::info 自动检测Alloy与Classic项目
该技能可自动检测项目类型,并提供针对性指导。
Alloy项目标识:
- 包含文件夹(MVC结构)
app/ - 包含、
app/views/文件夹app/controllers/ - 存在或
alloy.jmk文件config.json
Classic项目标识:
- 根目录下的文件夹中包含
Resources/app.js - 无文件夹结构
app/
对应行为:
- 检测到Alloy项目:提供Alloy MVC模式及Backbone.js相关指导
- 检测到Classic项目:避免仅适用于Alloy的模式,推荐Classic项目的实现方案或迁移建议
- 未知项目类型:请用户明确项目类型 :::
Workflow
工作流程
- Architecture: define structure by technical type with flat folders (,
lib/api,lib/services,lib/actions,lib/repositories)lib/helpers - Data strategy: choose Models (SQLite) or Collections (API)
- Contracts: define I/O specs between layers
- Implementation: write XML views and ES6+ controllers
- Quality: testing, error handling, logging, performance
- Cleanup: implement a pattern for memory management
cleanup()
- 架构设计:按技术类型定义扁平文件夹结构(、
lib/api、lib/services、lib/actions、lib/repositories)lib/helpers - 数据策略:选择Models(SQLite)或Collections(API)
- 契约定义:明确各层之间的输入输出规范
- 开发实现:编写XML视图与ES6+控制器
- 质量保障:测试、错误处理、日志记录、性能优化
- 内存清理:实现模式进行内存管理
cleanup()
Architectural Maturity Tiers
架构成熟度层级
Choose the appropriate tier based on project complexity:
根据项目复杂度选择合适的层级:
Tier 1: Basic (Rapid Prototyping)
层级1:基础版(快速原型开发)
- Best for: Simple utility apps or developers transitioning from Classic.
- Structure: Logic resides directly within .
index.js - UI Access: Direct usage of the object throughout the file.
$ - Pros: Zero boilerplate, extremely fast start.
- Cons: Unmaintainable beyond 500 lines of code.
- 适用场景:简单工具类应用,或从Classic模式过渡的开发者
- 结构:逻辑直接写在中
index.js - UI访问:在整个文件中直接使用对象
$ - 优势:无需任何模板代码,启动速度极快
- 劣势:代码量超过500行后难以维护
Tier 2: Intermediate (Modular Alloy)
层级2:进阶版(模块化Alloy)
- Best for: Standard commercial applications.
- Structure: Business logic extracted to using a flat technical-type organization.
app/lib/ - Pattern: Slim Controllers that services.
require() - Memory: Mandatory implementation of .
$.cleanup = cleanup
- 适用场景:标准商业应用
- 结构:业务逻辑提取至,按技术类型进行扁平组织
app/lib/ - 模式:轻量控制器,通过引入服务
require() - 内存管理:必须实现
$.cleanup = cleanup
Tier 3: Advanced / Enterprise (Service-Oriented)
层级3:高级版/企业版(面向服务)
- Best for: High-complexity apps (IDEs like TiDesigner, multi-state platforms).
- Architecture: Dependency Injection via a .
ServiceRegistry - ID Scoping: Services do not receive the entire object. They receive a "Scoped UI" object containing only relevant IDs.
$ - Cognitive Load: "Black Box" logic—encapsulated units that reduce mental fatigue.
- Observability: Structured logging with a mandatory context.
service
Detailed examples and full implementation samples are available in: Architectural Tiers Detail
- 适用场景:高复杂度应用(如TiDesigner这类IDE、多状态平台应用)
- 架构:通过实现依赖注入
ServiceRegistry - ID作用域:服务不接收完整的对象,仅接收包含相关ID的“作用域UI对象”
$ - 认知负担:“黑盒”逻辑——封装的单元可降低脑力消耗
- 可观测性:结构化日志,且必须包含上下文
service
详细示例与完整实现样例请查看:架构层级详情
Organization policy (low freedom)
组织规范(低自由度)
- Use technical-type organization in (for example:
lib,api,services,actions,repositories,helpers,policies).providers - Keep flat and predictable:
libonly.lib/<type>/<file>.js - Do not recommend deep nesting like .
lib/services/auth/session/login.js - Keep UI layers aligned by screen (,
controllers/,views/) and avoid unnecessary depth.styles/
- 在目录中按技术类型组织文件(例如:
lib、api、services、actions、repositories、helpers、policies)providers - 保持目录扁平且可预测:仅使用
lib结构lib/<type>/<file>.js - 不推荐深度嵌套,如
lib/services/auth/session/login.js - UI层按页面保持对齐(、
controllers/、views/),避免不必要的层级styles/
Code standards (low freedom)
代码规范(低自由度)
- No semicolons: let ASI handle it
- Modern syntax: , destructuring, template literals
const/let - : batch UI updates to reduce bridge crossings
applyProperties() - Memory cleanup: any controller with global listeners must set
$.cleanup = cleanup - Error handling: use AppError classes, log with context, never swallow errors
- Testable code: inject dependencies, avoid hard coupling
- 不使用分号:让ASI(自动分号插入)处理
- 使用现代语法:、解构赋值、模板字符串
const/let - 使用:批量更新UI以减少桥接交互
applyProperties() - 内存清理:任何包含全局监听器的控制器必须设置
$.cleanup = cleanup - 错误处理:使用AppError类,携带上下文日志,绝不吞掉错误
- 可测试代码:注入依赖,避免硬耦合
Titanium style sheets rules (low freedom)
Titanium样式表规则(低自由度)
:::danger Critical: platform-specific properties require modifiers
Using or properties without platform modifiers breaks cross-platform builds.
Ti.UI.iOS.*Ti.UI.Android.*Example of the damage:
tss
// Wrong: adds Ti.UI.iOS to Android project
"#mainWindow": {
statusBarStyle: Ti.UI.iOS.StatusBar.LIGHT_CONTENT
}Correct: always use platform modifiers
tss
// Correct: only adds to iOS
"#mainWindow[platform=ios]": {
statusBarStyle: Ti.UI.iOS.StatusBar.LIGHT_CONTENT
}Properties that always need platform modifiers:
- iOS: ,
statusBarStyle,modalStyle, anymodalTransitionStyleTi.UI.iOS.* - Android: configuration, any
actionBarconstantTi.UI.Android.*
Available modifiers: , , , ,
[platform=ios][platform=android][formFactor=handheld][formFactor=tablet][if=Alloy.Globals.customVar]For more platform-specific patterns, see the skill.
:::
ti-uiTitanium layout system:
- Three layout modes: ,
layout: 'horizontal', and composite (default, nolayout: 'vertical'needed)layout - No padding on container Views: use margins on children instead
- fills available space (preferred),
width: Ti.UI.FILL= 100% of parentwidth: '100%' - wraps content,
height: Ti.UI.SIZEfills available spaceheight: Ti.UI.FILL
:::danger 重要提示:平台专属属性必须使用修饰符
直接使用或属性而不加平台修饰符会破坏跨平台构建。
Ti.UI.iOS.*Ti.UI.Android.*错误示例:
tss
// 错误写法:会在Android项目中添加Ti.UI.iOS相关代码
"#mainWindow": {
statusBarStyle: Ti.UI.iOS.StatusBar.LIGHT_CONTENT
}正确写法:始终使用平台修饰符
tss
// 正确写法:仅在iOS项目中生效
"#mainWindow[platform=ios]": {
statusBarStyle: Ti.UI.iOS.StatusBar.LIGHT_CONTENT
}必须添加平台修饰符的属性:
- iOS:、
statusBarStyle、modalStyle,所有modalTransitionStyle属性Ti.UI.iOS.* - Android:配置,所有
actionBar常量Ti.UI.Android.*
可用修饰符:、、、、
[platform=ios][platform=android][formFactor=handheld][formFactor=tablet][if=Alloy.Globals.customVar]更多平台专属模式,请查看技能文档。
:::
ti-uiTitanium布局系统:
- 三种布局模式:、
layout: 'horizontal',以及复合模式(默认,无需设置layout: 'vertical')layout - 容器视图不设置内边距:改为在子元素上设置外边距
- 填充可用空间(推荐使用),
width: Ti.UI.FILL表示占父元素的100%宽度width: '100%' - 自适应内容高度,
height: Ti.UI.SIZE填充可用空间height: Ti.UI.FILL
Alloy builtins quick reference
Alloy内置对象速查
Key builtins: / (compile-time), (config.json), (shared state), (controller params), (cleanup bindings), / (XML conditionals).
OS_IOSOS_ANDROIDAlloy.CFGAlloy.Globals$.args$.destroy()platform="ios"formFactor="tablet"For the complete reference with examples, see Alloy builtins and globals.
核心内置对象:/(编译时)、(来自config.json)、(共享状态)、(控制器参数)、(清理绑定)、 / (XML条件判断)
OS_IOSOS_ANDROIDAlloy.CFGAlloy.Globals$.args$.destroy()platform="ios"formFactor="tablet"完整带示例的参考文档,请查看Alloy内置对象与全局变量。
Quick decision matrix
快速决策矩阵
| Question | Answer |
|---|---|
| How to create a new Alloy project? | |
| Fastest way to build? | |
| Controller > 100 lines? | Extract to Tier 2 (Services) |
| More than 50 IDs in XML? | Use Tier 3 (ID Scoping) |
| Where does API call go? | |
| Where does business logic go? | |
How deep should | One level: |
| Where do I store auth tokens? | Keychain (iOS) / KeyStore (Android) via service |
| Models or Collections? | Collections for API data, Models for SQLite persistence |
| Ti.App.fireEvent or EventBus? | Always EventBus (Backbone.Events) |
| Direct navigation or service? | Always Navigation service (auto cleanup) |
| Inline styles or TSS files? | Always TSS files (per-controller + |
| 问题 | 答案 |
|---|---|
| 如何创建新的Alloy项目? | |
| 最快的构建方式? | |
| 控制器代码超过100行? | 提取至层级2(服务层) |
| XML中包含超过50个ID? | 使用层级3(ID作用域) |
| API请求代码放在哪里? | |
| 业务逻辑放在哪里? | |
| 仅一层: |
| 在哪里存储认证令牌? | 通过服务存储到iOS的Keychain或Android的KeyStore中 |
| 选Models还是Collections? | API数据用Collections,SQLite持久化用Models |
| 用Ti.App.fireEvent还是EventBus? | 始终使用EventBus(Backbone.Events) |
| 直接导航还是用服务? | 始终使用导航服务(自动清理) |
| 内联样式还是TSS文件? | 始终使用TSS文件(按控制器划分 + 全局的 |
Reference guides (progressive disclosure)
参考指南(渐进式披露)
Architecture & Patterns
架构与模式
- Architectural Tiers Detail
- Architectural Patterns (Factory, Singleton, Repository)
- Structure & Organization
- Contracts & Communication
- State Management
- Anti-patterns to Avoid
- 架构层级详情
- 架构模式(工厂模式、单例模式、仓库模式)
- 结构与组织
- 契约与通信
- 状态管理
- 需避免的反模式
Implementation & API
实现与API
- Alloy Builtins & Globals
- Code Conventions
- Controller Patterns
- Theming & Dark Mode
- Migration Patterns
- Examples Collection
- Alloy内置对象与全局变量
- 代码规范
- 控制器模式
- 主题与暗黑模式
- 迁移模式
- 示例集合
Quality & Performance
质量与性能
- Performance Optimization
- ListView & ScrollView Performance
- Error Handling & Logging
- Unit & Integration Testing
- E2E Testing & CI/CD
- 性能优化
- ListView与ScrollView性能优化
- 错误处理与日志
- 单元与集成测试
- 端到端测试与CI/CD
Security
安全
- Security Fundamentals
- Device Security
- 安全基础
- 设备安全
Tools
工具
- CLI Expert & TiNy
- CLI专家与TiNy