Przeglądaj źródła

Level 1.1: non-default parse() during fetch().

- use Backbone 0.9.9 instead of 1.3.3 to match course
Frederic G. MARAND 6 lat temu
rodzic
commit
d266c29f2a

+ 1 - 1
.idea/.name

@@ -1 +1 @@
-CodeSchool Backbone 1
+CodeSchool Backbone

+ 0 - 0
.idea/CodeSchool Backbone 1.iml → .idea/CodeSchool Backbone.iml


+ 10 - 0
.idea/bb-codeschool.iml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 1 - 1
.idea/modules.xml

@@ -2,7 +2,7 @@
 <project version="4">
   <component name="ProjectModuleManager">
     <modules>
-      <module fileurl="file://$PROJECT_DIR$/.idea/CodeSchool Backbone 1.iml" filepath="$PROJECT_DIR$/.idea/CodeSchool Backbone 1.iml" />
+      <module fileurl="file://$PROJECT_DIR$/.idea/bb-codeschool.iml" filepath="$PROJECT_DIR$/.idea/bb-codeschool.iml" />
     </modules>
   </component>
 </project>

+ 2 - 2
.idea/runConfigurations/npm_build.xml → .idea/runConfigurations/NPM_Watch.xml

@@ -1,9 +1,9 @@
 <component name="ProjectRunConfigurationManager">
-  <configuration default="false" name="npm build" type="js.build_tools.npm" factoryName="npm">
+  <configuration default="false" name="NPM Watch" type="js.build_tools.npm" factoryName="npm">
     <package-json value="$PROJECT_DIR$/web/client/package.json" />
     <command value="run" />
     <scripts>
-      <script value="build" />
+      <script value="watch" />
     </scripts>
     <node-interpreter value="project" />
     <envs />

+ 0 - 9
.idea/runConfigurations/npm_Install.xml

@@ -1,9 +0,0 @@
-<component name="ProjectRunConfigurationManager">
-  <configuration default="false" name="npm Install" type="js.build_tools.npm" factoryName="npm">
-    <package-json value="$PROJECT_DIR$/web/client/package.json" />
-    <command value="install" />
-    <node-interpreter value="project" />
-    <envs />
-    <method />
-  </configuration>
-</component>

+ 11 - 1
src/Controllers/TodosController.php

@@ -16,7 +16,7 @@ class TodosController {
     return $app->json(array_values($todos));
   }
 
-  public function get(Application $app, $id) {
+  public function getLevel1(Application $app, $id) {
     /** @var \Model $model */
     $model = $app['model'];
     $todos = $model->load();
@@ -24,6 +24,16 @@ class TodosController {
     return $app->json($todo);
   }
 
+  public function getLevel2(Application $app, $id) {
+    /** @var \Model $model */
+    $model = $app['model'];
+    $todos = $model->load();
+    $todo = $todos[$id]
+      ? ['todo' => $todos[$id]]
+      : NULL;
+    return $app->json($todo);
+  }
+
   public function put(Application $app, Request $request, int $id) {
     $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
     return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);

+ 0 - 2
src/Model.php

@@ -1,7 +1,5 @@
 <?php
 
-use Symfony\Component\HttpFoundation\Response;
-
 class Model {
   protected $data = [];
   protected $path;

+ 1 - 1
src/routing.php

@@ -4,7 +4,7 @@ use Controllers\ServerController;
 use Controllers\TodosController;
 
 $app->get('/todos',         TodosController::class . '::index');
-$app->get('/todos/{id}',    TodosController::class . '::get');
+$app->get('/todos/{id}',    TodosController::class . '::getLevel2');
 $app->put('/todos/{id}',    TodosController::class . '::put');
 $app->post('/todos',        TodosController::class . '::post')
   ->value('id', NULL);

+ 3 - 3
web/client/package-lock.json

@@ -500,9 +500,9 @@
       "dev": true
     },
     "backbone": {
-      "version": "1.3.3",
-      "resolved": "https://registry.npmjs.org/backbone/-/backbone-1.3.3.tgz",
-      "integrity": "sha1-TMgOp8sWMaxHSInOQPL4vGg7KZk=",
+      "version": "0.9.9",
+      "resolved": "https://registry.npmjs.org/backbone/-/backbone-0.9.9.tgz",
+      "integrity": "sha1-CGpo3Q69J6jlQS+RX7Wg3ABinrc=",
       "requires": {
         "underscore": "1.8.3"
       }

+ 3 - 2
web/client/package.json

@@ -1,7 +1,7 @@
 {
   "author": "CodeSchool",
   "dependencies": {
-    "backbone": "^1.3.3",
+    "backbone": "0.9.9",
     "jquery": "^3.2.1",
     "json2": "^0.4.0",
     "lodash": "^4.17.4"
@@ -25,7 +25,8 @@
   },
   "scripts": {
     "build": "babel src -d lib --source-maps",
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "watch": "babel --watch src -d lib --source-maps"
   },
   "version": "1.0.0"
 }

+ 28 - 0
web/client/src/a.js

@@ -0,0 +1,28 @@
+const AppRouter = new Backbone.Router({
+  routes: { "appointments/:id": "show", "": "index" },
+
+  initialize: function(options){
+    this.appointmentList = options.appointmentList;
+  },
+
+  index: function(){
+    var appointmentsView = new AppointmentListView({collection: this.appointmentList});
+    appointmentsView.render();
+    $('#app').html(appointmentsView.el);
+    this.appointmentList.fetch();
+  },
+
+  show: function(id){
+    var appointment = new Appointment({id: id});
+    var appointmentView = new AppointmentView({model: appointment});
+    appointmentView.render();
+    $('#app').html(appointmentView.el);
+    appointment.fetch();
+  },
+
+  start: function() {
+    Backbone.history.start({pushState: true});
+  },
+});
+
+AppRouter.start();

+ 2 - 2
web/client/src/todoListView.js

@@ -47,6 +47,6 @@ function todoViewListExample(list) {
 }
 
 $(function () {
-  globalList = todoListFromServer();
-  globalListView = todoViewListExample(globalList);
+  // globalList = todoListFromServer();
+  // globalListView = todoViewListExample(globalList);
 });

+ 6 - 1
web/client/src/todoModel.js

@@ -7,6 +7,11 @@ const TodoItem = Backbone.Model.extend({
     // RESTful web service, RoR flavor.
     urlRoot: '/server/todos',
 
+    // Default parse() implementation, not needed with RoR style APIs
+    // parse: response => response,
+
+    parse: (response) => response.todo,
+
     toggleStatus(view) {
       this.set('status', (this.get('status') === 'incomplete')
         ? 'complete'
@@ -30,7 +35,7 @@ function todoModelSingleExample() {
   // Skip listener during a change. Notice the value format.
   todoItem.set({ 'description': 'some other description' }, {silent: true});
   // Remove listener.
-  //todoItem.off('change', onChange);
+  todoItem.off('change', onChange);
 
   /* Built-in events
   change - when any attribute is modified

+ 5 - 5
web/client/yarn.lock

@@ -362,11 +362,11 @@ babylon@^6.18.0:
   version "6.18.0"
   resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"
 
-backbone@^1.3.3:
-  version "1.3.3"
-  resolved "https://registry.yarnpkg.com/backbone/-/backbone-1.3.3.tgz#4cc80ea7cb1631ac474889ce40f2f8bc683b2999"
+backbone@0.9.9:
+  version "0.9.9"
+  resolved "https://registry.yarnpkg.com/backbone/-/backbone-0.9.9.tgz#086a68dd0ebd27a8e5412f915fb5a0dc00629eb7"
   dependencies:
-    underscore ">=1.8.3"
+    underscore ">=1.4.3"
 
 balanced-match@^1.0.0:
   version "1.0.0"
@@ -2026,7 +2026,7 @@ uid-number@^0.0.6:
   version "0.0.6"
   resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
 
-underscore@>=1.8.3:
+underscore@>=1.4.3:
   version "1.8.3"
   resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.8.3.tgz#4f3fb53b106e6097fcf9cb4109f2a5e9bdfa5022"
 

+ 6 - 6
web/data/items.json

@@ -1,4 +1,9 @@
 {
+    "1": {
+        "id": 1,
+        "description": "some description",
+        "status": "incomplete"
+    },
     "2": {
         "status": "incomplete",
         "description": "second",
@@ -8,10 +13,5 @@
         "status": "incomplete",
         "description": "third",
         "id": 3
-    },
-    "1": {
-        "description": "some other description",
-        "status": "complete",
-        "id": 1
     }
-}
+}