ruby-on-rails 嵌套的刺激HTML防止传递值

jm81lzqq  于 2023-03-24  发布在  Ruby
关注(0)|答案(2)|浏览(151)

使用以下命令,我无法访问控制器中的值

<div data-controller="static-get-coordinates">
    <div id="sgcmap"
        data-static-get-coordinates-url-value = "<%= url %>"
        data-static-get-coordinates-coords-value = "<%= snippetCoords %>"
        data-static-get-coordinates-center-value = "<%= snippetCenter %>"
        data-static-get-coordinates-xcenter-value = "<%= centerX %>" 
        data-static-get-coordinates-ycenter-value = "<%= centerY %>"
        style="width: 1024px; height: 968px"
        class="mt-1"
    ></div>
    <span>Drawn Coordinates:</span>
    <div id="sgcresult" data-static-get-coordinates-target="drawnCoordinates">
        after drawing a rectangle of course
    </div>
    <div class="fst-italic" data-static-get-coordinates-target="output"></div>
</div> <!-- data-controller div -->

但是如果我把它解嵌套,这样就可以访问值了。

<div data-controller="static-get-coordinates" id="sgcmap"
    data-static-get-coordinates-url-value = "<%= url %>"
    data-static-get-coordinates-coords-value = "<%= snippetCoords %>"
    data-static-get-coordinates-center-value = "<%= snippetCenter %>"
    data-static-get-coordinates-xcenter-value = "<%= centerX %>" 
    data-static-get-coordinates-ycenter-value = "<%= centerY %>"
    style="width: 1024px; height: 968px"
    class="mt-1"
></div>

我看过嵌套的例子,但它是针对目标的,而不是值。有没有办法解决这个问题?如果我像第二个一样创建两个,url引用的图像会显示两次。(在这个例子中,一些变量(值)没有被使用,但它们在我的应用程序中,这是一个简化的页面来解决问题。这个例子是可用的here。目前它有硬编码的“值”,但是可以绘制矩形,并且坐标出现在页面上。矩形不会像它应该的那样停留在页面上。

// app/javascript/controllers/static_get_coordinates.js
import { Controller } from "@hotwired/stimulus"

import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {getCenter} from 'ol/extent';
import Projection from 'ol/proj/Projection';
import Static from 'ol/source/ImageStatic';
import ImageLayer from 'ol/layer/Image';
import Draw, {createBox} from 'ol/interaction/Draw.js';
import {Vector as VectorSource} from 'ol/source.js';
import {Tile as TileLayer} from 'ol/layer.js';

export default class extends Controller {
  static targets = [ "drawnCoordinates" ]

  static values = {
    url: String,
    coords: Array,
    xcenter: Number,
    ycenter: Number,
    center: Array,
  }
  connect(){
    // var xkcdUrl = this.urlValue;
    // const center = this.centerValue;
   //  var xCenter = this.xcenterValue;
    const source = new VectorSource({wrapX: false});
 
    const extent = [0, 0, 1024, 968];
    const projection = new Projection({
      code: 'xkcd-image',
      units: 'pixels',
      extent: extent,
    });
 
    const xkcdLayer = new ImageLayer({
       source: new Static({
         attributions: '© <a href="https://xkcd.com/license.html">xkcd</a>',
         url: 'https://imgs.xkcd.com/comics/online_communities.png',
         // url: xkcdUrl,
         projection: projection,
         imageExtent: extent,
       }),
     });
    var map = new Map({
      layers: [
         xkcdLayer
      ],
      target: 'sgcmap',
      view: new View({
        projection: projection,
        center: getCenter(extent),
        zoom: 2,
        maxZoom: 8,
      }),
    });
    function addDrawInteraction(coordsOutputTarget) {
      let draw = new Draw({
         source: source,
         type: 'Circle', // I think the circle is the cursor
         geometryFunction: createBox(), // this makes the drawn object a rectangle
       });
       map.addInteraction(draw);
   
       draw.on('drawend', function(event) {
         let boxCoords = event.feature.values_.geometry.flatCoordinates; 
         const boxCoordsTrunc = boxCoords.map(x => Math.trunc(x)) // For readabililty truncating the 13 meaningless digits after the decimal place. 
         coordsOutputTarget.textContent = `${boxCoordsTrunc}`;
      }); 
     } 
      addDrawInteraction(this.drawnCoordinatesTarget); // sending target for coordinates and activating the ability to draw a rectangle and get its coordinate
  }
}
e0bqpujr

e0bqpujr1#

根据我自己阅读和尝试的内容,您需要将值写入控制器元素(与您拥有data-controller属性的元素相同),如documentation中所述。您的控制器应该只控制 something 的一个示例,因此您必须将值放入控制器元素中并不重要。
如果你熟悉其他JavaScript框架,比如React或Vue,我想你可以把Stimulus控制器想象成沿着于React组件的东西:它里面有一些HTML(也许还有一些子元素),你可以给它传递一些props(或Stimulus中的值)。HTML和JavaScript只是在Stimulus中被分割到不同的文件中,仅此而已。就像如果你有一个Cart组件,它可以有一个totalSum = 0的初始值。然后购物车可以有多个子组件,让我们称之为CartItems,添加或删除CartItem(或修改其属性,如quantity)将更新Cart组件的totalSum
我认为你需要做的是把你的控制器逻辑分成两部分(如果没有更多的上下文,很难确切地说,但这就是它看起来的样子)。使用现有的一个作为外部控制器,它可以控制现有的目标等。它看起来像你正在处理某种Map数据,所以假设内部的一个只控制单个Map的数据:将特定于map的值放到每个map的控制器中,如果有任何值不是,则将它们分配给外部控制器。然后可以通过各种方式在这些控制器之间发送数据,例如outlets

vptzau2j

vptzau2j2#

也发表了here at fly.io
超文本标记语言

<div data-controller="static-get-coordinates" id="sgcmap"
    data-static-get-coordinates-url-value = "<%= url %>"
    data-static-get-coordinates-coords-value = "<%= snippetCoords %>"
    data-static-get-coordinates-center-value = "<%= snippetCenter %>"
    data-static-get-coordinates-xcenter-value = "<%= centerX %>" 
    data-static-get-coordinates-ycenter-value = "<%= centerY %>"
    style="width: 1024px; height: 968px"
    class="mt-1">
    <div id="sgcresult" data-static-get-coordinates-target="drawnCoordinates">Drawn coordinates will appear here after drawing a rectangle</div>
</div>

//app/javascript/controllers/static_get_coordinates.js

import { Controller } from "@hotwired/stimulus"

import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {getCenter} from 'ol/extent';
import Projection from 'ol/proj/Projection';
import Static from 'ol/source/ImageStatic';
import ImageLayer from 'ol/layer/Image';
import Draw, {createBox} from 'ol/interaction/Draw.js';
import {Vector as VectorSource} from 'ol/source.js';
import {Tile as TileLayer} from 'ol/layer.js';
    
export default class extends Controller {
    static targets = [ "drawnCoordinates" ]
    
    static values = {
        url: String,
        coords: Array,
        xcenter: Number,
        ycenter: Number,
        center: Array,
    }
    connect(){
        var xkcdUrl = this.urlValue; // this is the only value I'm using in this demo. But other descriptors of the image in the final app
        
        const source = new VectorSource({wrapX: false});
         
        const extent = [0, 0, 1024, 968];
        const projection = new Projection({
            code: 'xkcd-image',
            units: 'pixels',
            extent: extent,
        });
        
        const xkcdLayer = new ImageLayer({
             source: new Static({
                 attributions: '© <a href="https://xkcd.com/license.html">xkcd</a>',
                 url: xkcdUrl,
                projection: projection,
                 imageExtent: extent,
             }),
         });
        var map = new Map({
            layers: [
                 xkcdLayer
            ],
            target: 'sgcmap',
            view: new View({
                projection: projection,
                center: getCenter(extent),
                zoom: 2,
                maxZoom: 8,
            }),
        });
        function addDrawInteraction(coordsOutputTarget) {
            let draw = new Draw({
                 source: source,
                 type: 'Circle',
                 geometryFunction: createBox(),
             });
             map.addInteraction(draw);
             draw.on('drawend', function(event) {
                let boxCoords = event.feature.values_.geometry.flatCoordinates;
                const boxCoordsTrunc = boxCoords.map(x => Math.trunc(x)) // For readability truncating the 13 meaningless digits after the decimal place 
                    coordsOutputTarget.textContent = `${boxCoordsTrunc}`;
            }); 
        }
        addDrawInteraction(this.drawnCoordinatesTarget); // sending target for coordinates and having this function available
     }
}

相关问题