diff --git a/examples/ReactExample/index.ios.js b/examples/ReactExample/index.ios.js
index 5c393c26..707f2246 100644
--- a/examples/ReactExample/index.ios.js
+++ b/examples/ReactExample/index.ios.js
@@ -9,90 +9,196 @@ var React = require('react-native');
var {
AppRegistry,
StyleSheet,
+ NavigatorIOS,
+ AlertIOS,
ListView,
+ TouchableHighlight,
Text,
+ TextInput,
View,
} = React;
-var Person = function() {}
-Person.prototype.schema = {
- name: 'Person',
+var TodoItemSchema = {
+ name: 'Todo',
properties: [
- {name: 'name', type: RealmType.String},
- {name: 'age', type: RealmType.Double},
+ {name: 'text', type: RealmType.String},
]
};
-Person.prototype.capitalName = function() {
- return this.name.toUpperCase();
+var TodoListSchmea = {
+ name: 'TodoList',
+ properties: [
+ {name: 'name', type: RealmType.String},
+ {name: 'items', type: RealmType.Array, objectType: 'Todo'}
+ ]
};
-var RealmReact = React.createClass({
- getInitialState: function() {
- return {
- dataSource: new ListView.DataSource({
- rowHasChanged: (row1, row2) => row1 !== row2,
- }),
- };
- },
- renderPerson: function(person) {
- return (
-
- {person.capitalName()} {person.age}
-
- );
- },
- render: function() {
- return (
-
-
-
- Press Cmd+R to reload,{'\n'}
- Cmd+Control+Z for dev menu
-
-
- );
- },
- componentDidMount: function() {
- var realm = new Realm({schema: [Person]});
- var objects = realm.objects('Person');
-
- console.log(realm.path);
+console.log(Realm.defaultPath);
+var realm = new Realm({schema: [TodoItemSchema, TodoListSchmea]});
- if (objects.length < 4) {
- realm.write(function() {
- realm.create('Person', ['Joe', 26]);
- realm.create('Person', ['Sam', 20]);
- realm.create('Person', ['Alexander', 37]);
- realm.create('Person', ['Ari', 34]);
- realm.create('Person', {'name': 'Bjarne', age: 37});
- });
+class Edit extends React.Component {
+ componentWillMount() {
+ this.setState({text: this.props.text});
}
- var olderThan30 = realm.objects('Person', 'age > 30');
- console.log(olderThan30.length);
+ save() {
+ var list = this.props.list;
+ var id = this.props.todoId;
+ var text = this.state.text;
+ realm.write(function () {
+ if (id == list.items.length) {
+ list.items.push({text: text});
+ }
+ else {
+ var todoItem = list.items[id];
+ todoItem.text = text;
+ }
+ });
+ // should not be needed once we have notifications
+ this.props.parent.updateDataSource();
+ this.props.navigator.pop();
+ }
- this.setState({
- dataSource: this.state.dataSource.cloneWithRows(olderThan30),
- });
- },
-});
+ render() {
+ return (
+
+ this.setState({text})} value={this.state.text}/>
+
+ Save
+
+
+ )
+ }
+};
+
+class TodoList extends React.Component {
+ componentWillMount() {
+ this.lists = realm.objects('TodoList');
+ if (this.lists.length < 1) {
+ realm.write(function() {
+ this.list = realm.create('TodoList', ['List', []]);
+ });
+ }
+ else {
+ this.list = this.lists[0];
+ }
+ this.menu = this.menu.bind(this);
+ this.delete = this.delete.bind(this);
+ var dataSource = new ListView.DataSource({
+ rowHasChanged: (row1, row2) => row1 !== row2
+ });
+
+ this.updateDataSource(dataSource);
+ }
+
+ updateDataSource(oldDataSource) {
+ if (!oldDataSource) {
+ oldDataSource = this.state.dataSource;
+ }
+ this.setState({dataSource: oldDataSource.cloneWithRows(this.list.items)});
+ }
+
+ menu(todo, todoID) {
+ AlertIOS.alert(
+ todo.text,
+ todoID,
+ [
+ {text: 'Complete', onPress: () => this.delete(todoID)},
+ {text: 'Edit', onPress: () => this.edit(todoID, todo.text)},
+ {text: 'Cancel'}
+ ]
+ )
+ }
+
+ delete(todoID) {
+ var item = this.list.items[todoID];
+ realm.write(function() {
+ realm.delete(item);
+ })
+ this.updateDataSource();
+ }
+
+ edit(todoId, text) {
+ this.props.navigator.push({
+ title: text,
+ component: Edit,
+ passProps: {list: this.list, todoId: todoId, text: text, parent: this}
+ });
+ }
+
+ render() {
+ return (
+
+
+ this.menu(rowData, rowID)}>
+
+ {rowData.text}
+
+
+ }/>
+ this.edit(this.list.items.length, "")}>
+ +
+
+
+ Press Cmd+R to reload,{'\n'}
+ Cmd+Control+Z for dev menu
+
+
+ );
+ }
+};
+
+class Navigator extends React.Component {
+ render() {
+ return (
+
+ );
+ }
+};
+AppRegistry.registerComponent('ReactExample', () => Navigator);
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
- backgroundColor: '#F5FCFF',
+ backgroundColor: '#ffffff',
+ },
+ listItem: {
+ marginTop: 3,
+ padding: 2,
+ backgroundColor:'#ABABAB',
+ alignSelf: 'stretch',
+ flexDirection: 'row',
+ flex:1,
+ },
+ textInput: {
+ alignSelf: 'stretch',
+ borderWidth: 0.5,
+ borderColor: '#0f0f0f',
+ height: 200,
+ fontSize: 13,
+ margin: 6,
+ marginTop: 70,
+ padding: 4,
+ },
+ button: {
+ height: 36,
+ backgroundColor: '#48BBEC',
+ alignSelf: 'stretch',
+ justifyContent: 'center'
+ },
+ buttonText: {
+ alignSelf: 'center',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
- },
- listView: {
- paddingTop: 20,
- backgroundColor: '#F5FCFF',
- },
+ }
});
-
-AppRegistry.registerComponent('ReactExample', () => RealmReact);
diff --git a/src/RJSArray.cpp b/src/RJSArray.cpp
index 69f97d39..38004fe9 100644
--- a/src/RJSArray.cpp
+++ b/src/RJSArray.cpp
@@ -18,6 +18,7 @@
#include "RJSArray.hpp"
#include "RJSObject.hpp"
+#include "RJSUtil.hpp"
#include "object_accessor.hpp"
using namespace realm;
@@ -79,11 +80,29 @@ void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccu
}
}
+JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
+ try {
+ ObjectArray *array = RJSGetInternal(thisObject);
+ RJSValidateArgumentCount(argumentCount, 1);
+ array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast(arguments[0]), array->object_schema.name, false));
+ }
+ catch (std::exception &exp) {
+ if (jsException) {
+ *jsException = RJSMakeError(ctx, exp);
+ }
+ }
+ return NULL;
+}
+
JSObjectRef RJSArrayCreate(JSContextRef ctx, realm::ObjectArray *array) {
return RJSWrapObject(ctx, RJSArrayClass(), array);
}
JSClassRef RJSArrayClass() {
- static JSClassRef s_objectClass = RJSCreateWrapperClass