{"id":981,"date":"2019-06-15T22:36:37","date_gmt":"2019-06-15T13:36:37","guid":{"rendered":"https:\/\/ito-u-oti.com\/?p=981"},"modified":"2019-06-15T22:36:40","modified_gmt":"2019-06-15T13:36:40","slug":"post-981","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=981","title":{"rendered":"\u3010RequireJS\u3011\u3010BackboneJS\u3011\u30b5\u30fc\u30d0\u304b\u3089\u53d6\u5f97\u3057\u305f\u5024\u3092\u8868\u793a\u3059\u308b"},"content":{"rendered":"\n<p>\u30b5\u30fc\u30d0\u5074\u306eREST API\u3092\u53e9\u3044\u3066\u53d6\u5f97\u3057\u305f\u5024\u3092View\u3067\u753b\u9762\u4e0a\u306b\u8868\u793a\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__1\" class=\"wp-block-heading\">\u524d\u63d0<\/h2>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\">\u6982\u8981<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Model\u304b\u3089REST API\u3092\u53e9\u304d\u3001\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/li><li>View\u306fModel\u304c\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u753b\u9762\u4e0a\u306b\u8868\u793a\u3055\u305b\u308b\u3002<\/li><\/ul>\n\n\n\n<h3 id=\"outline__1_2\" class=\"wp-block-heading\">\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"299\" height=\"292\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2019\/06\/018_backboneJS1.png\" alt=\"\" class=\"wp-image-1007\"\/><\/figure>\n\n\n\n<h3 id=\"outline__1_3\" class=\"wp-block-heading\">\u30b5\u30fc\u30d0\u5074\u60c5\u5831<\/h3>\n\n\n\n<h4 id=\"outline__1_3_1\" class=\"wp-block-heading\">REST URL<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncurl -X GET http:\/\/localhost:3000\/hello\n<\/pre><\/div>\n\n\n<h4 id=\"outline__1_3_2\" class=\"wp-block-heading\">\u53d6\u5f97\u30c7\u30fc\u30bf<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  &quot;text&quot;: &quot;Hello World Backbone Fetch!!&quot;\n}\n<\/pre><\/div>\n\n\n<h2 id=\"outline__2\" class=\"wp-block-heading\">\u30b5\u30fc\u30d0\u304b\u3089\u5024\u3092\u53d6\u5f97\u3057\u3066\u8868\u793a\u3059\u308b<\/h2>\n\n\n\n<h3 id=\"outline__2_1\" class=\"wp-block-heading\">index.html<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;ja&quot;&gt;\n    &lt;head&gt;\n        &lt;meta charset=&quot;utf-8&quot;&gt;\n        &lt;title&gt;&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;script src=&quot;https:\/\/requirejs.org\/docs\/release\/2.3.6\/minified\/require.js&quot;&gt;&lt;\/script&gt;\n        &lt;script src=&quot;.\/js\/config\/config.js&quot;&gt;&lt;\/script&gt;\n        &lt;script src=&quot;.\/js\/views\/HelloWorldView.js&quot;&gt;&lt;\/script&gt;\n        &lt;script type=&quot;text\/template&quot; id=&quot;hello-template&quot;&gt;\n            &lt;h2&gt;&lt;%= text %&gt;&lt;\/h2&gt;\n        &lt;\/script&gt;\n        &lt;h1&gt;Hello World!&lt;\/h1&gt;\n        &lt;div class=&quot;hoge&quot;&gt;&lt;\/div&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<h3 id=\"outline__2_2\" class=\"wp-block-heading\">config.js<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nrequire.config({\n    baseUrl: &quot;js&quot;,\n    paths: {\n        &quot;backbone&quot; : &quot;lib\/backbone&quot;,\n        &quot;jquery&quot; : &quot;lib\/jquery&quot;,\n        &quot;underscore&quot; : &quot;lib\/underscore&quot;\n    },\n    shim: {\n        'backbone' : {\n            deps: &#x5B;'underscore', 'jquery'],\n            exports: 'Backbone'\n        }\n    }\n});\n<\/pre><\/div>\n\n\n<h3 id=\"outline__2_3\" class=\"wp-block-heading\">HelloWorldModel.js<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ndefine(&#x5B;'backbone'], function(backbone) {\n\n    var HelloWorldModel = backbone.Model.extend({\n        url: &quot;http:\/\/localhost:3000\/hello&quot;,\n        defaults: {\n            &quot;text&quot;:&quot;Hello World Backbone!!&quot;\n        },\n        read: function(){\n            this.fetch()\n        }\n    });\n    return HelloWorldModel;\n});\n<\/pre><\/div>\n\n\n<p>Model\u306eread\u3092\u547c\u3073\u51fa\u3059\u3068\u3001fetch\u3067GET\u30e1\u30bd\u30c3\u30c9\u3092\u98db\u3070\u3057\u307e\u3059\u3002<br>\u53d6\u5f97\u3057\u305f\u5024\u306fModel\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002<br>\u4eca\u56de\u306e\u4f8b\u3067\u3044\u304f\u3068\u3001\u53d6\u5f97\u3059\u308b\u30ad\u30fc\u306ftext\u3067\u3042\u308a\u65e2\u306bModel\u4e0a\u306b\u5b58\u5728\u3059\u308b\u305f\u3081\u4e0a\u66f8\u304d\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__2_4\" class=\"wp-block-heading\">HelloWorldView.js<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nrequire(&#x5B;'backbone', 'model\/HelloWorldModel'], function(backbone, helloWorldModel) {\n\n    var model = new helloWorldModel();\n\n    var HelloWorldView = backbone.View.extend({\n        el: $('.hoge'),\n        initialize: function() {\n            model.read();\n            this.listenToOnce( model, 'sync', this.render);\n        },\n        render: function() {\n            var text = model.get(&quot;text&quot;);\n            $(this.el).html(_.template($('#hello-template').text())({ &quot;text&quot;: text }));\n        }\n    });\n    new HelloWorldView();\n});\n<\/pre><\/div>\n\n\n<ol class=\"wp-block-list\"><li>model\u306eread\u3092\u547c\u3073\u51fa\u3057\u3001\u30b5\u30fc\u30d0\u304b\u3089\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b<\/li><li>listenToOnce\u3067model\u306e\u72b6\u614b\u3092\u76e3\u8996\u3057\u3001model\u304c\u5909\u5316\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067View\u306erender\u3092\u547c\u3073\u51fa\u3059<\/li><li>read\u3057\u305f\u6642\u70b9\u3067model\u306etext\u304c\u4e0a\u66f8\u304d\u3055\u308c\u3001model\u306e\u72b6\u614b\u304c\u5909\u308f\u3063\u305f\u305f\u3081render\u304c\u547c\u3073\u51fa\u3055\u308c\u3001template\u304c\u753b\u9762\u306b\u633f\u5165\u3055\u308c\u308b<\/li><\/ol>\n\n\n\n<h3 id=\"outline__2_5\" class=\"wp-block-heading\">\u8868\u793a<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"738\" height=\"452\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2019\/06\/018_1_backboneJS1.png\" alt=\"\" class=\"wp-image-1008\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d0\u5074\u306eREST API\u3092\u53e9\u3044\u3066\u53d6\u5f97\u3057\u305f\u5024\u3092View\u3067\u753b\u9762\u4e0a\u306b\u8868\u793a\u3055\u305b\u307e\u3059\u3002 \u524d\u63d0 \u6982\u8981 Mode [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":972,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[220,227,213,226],"class_list":["post-981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-backbonejs","tag-fetch","tag-requirejs","tag-server"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=981"}],"version-history":[{"count":5,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":1009,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions\/1009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/media\/972"}],"wp:attachment":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}